Skip to content

Commit 4714e6a

Browse files
committed
优化 OnResBean 与 OnResList 便捷回调调用机制,修复其在 Android 上调用失败的问题:troyzhxu#61
1 parent cb23c9e commit 4714e6a

1 file changed

Lines changed: 8 additions & 35 deletions

File tree

okhttps/src/main/java/com/ejlchina/okhttps/AHttpTask.java

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
import com.ejlchina.data.Mapper;
55
import com.ejlchina.data.TypeRef;
66
import com.ejlchina.okhttps.HttpResult.State;
7-
import com.ejlchina.okhttps.internal.*;
7+
import com.ejlchina.okhttps.internal.AbstractHttpClient;
8+
import com.ejlchina.okhttps.internal.CopyInterceptor;
9+
import com.ejlchina.okhttps.internal.RealHttpResult;
810
import okhttp3.Call;
911
import okhttp3.Callback;
1012
import okhttp3.Response;
1113

1214
import java.io.IOException;
13-
import java.lang.reflect.InvocationTargetException;
14-
import java.lang.reflect.Method;
1515
import java.lang.reflect.Type;
1616
import java.util.List;
1717
import java.util.concurrent.CountDownLatch;
1818
import java.util.function.Consumer;
1919

20-
2120
/**
2221
* 异步 Http 请求任务
2322
*
@@ -431,14 +430,15 @@ interface ResponseCallback {
431430
void on(Runnable runnable, boolean onIo);
432431
}
433432

433+
@SuppressWarnings("unchecked")
434434
private synchronized Consumer<HttpResult> complexOnResponse(OkHttpCall call) {
435435
return res -> {
436436
Consumer<HttpResult> onResp = onResponse;
437437
Consumer<HttpResult.Body> onBody = onResBody;
438438
Consumer<Mapper> onMapper = onResMapper;
439439
Consumer<Array> onArray = onResArray;
440-
Consumer<?> onBean = onResBean;
441-
Consumer<?> onList = onResList;
440+
Consumer<Object> onBean = (Consumer<Object>) onResBean;
441+
Consumer<Object> onList = (Consumer<Object>) onResList;
442442
Consumer<String> onString = onResString;
443443

444444
int count = 0;
@@ -494,23 +494,11 @@ public void run() {
494494
}
495495
if (onBean != null) {
496496
Object bean = body.toBean(beanType);
497-
callback.on(() -> {
498-
try {
499-
callbackMethod(onBean.getClass(), bean.getClass()).invoke(onBean, bean);
500-
} catch (IllegalAccessException | InvocationTargetException e) {
501-
throw new OkHttpsException("回调方法调用失败!", e);
502-
}
503-
}, resBeanOnIO);
497+
callback.on(() -> onBean.accept(bean), resBeanOnIO);
504498
}
505499
if (onList != null) {
506500
List<?> list = body.toList(listType);
507-
callback.on(() -> {
508-
try {
509-
callbackMethod(onList.getClass(), list.getClass()).invoke(onList, list);
510-
} catch (IllegalAccessException | InvocationTargetException e) {
511-
throw new OkHttpsException("回调方法调用失败!", e);
512-
}
513-
}, resListOnIO);
501+
callback.on(() -> onList.accept(list), resListOnIO);
514502
}
515503
if (onString != null) {
516504
String string = body.toString();
@@ -519,21 +507,6 @@ public void run() {
519507
};
520508
}
521509

522-
static final String OnCallbackMethod = Consumer.class.getDeclaredMethods()[0].getName();
523-
524-
private Method callbackMethod(Class<?> clazz, Class<?> paraType) {
525-
Method[] methods = clazz.getDeclaredMethods();
526-
for (Method method : methods) {
527-
Class<?>[] paraTypes = method.getParameterTypes();
528-
if (method.getName().equals(OnCallbackMethod) && paraTypes.length == 1
529-
&& paraTypes[0].isAssignableFrom(paraType)) {
530-
method.setAccessible(true);
531-
return method;
532-
}
533-
}
534-
throw new IllegalStateException("没有可调用的方法");
535-
}
536-
537510
private void initBeanType(Type type) {
538511
if (type == null) {
539512
throw new IllegalArgumentException(" bean type can not be null!");

0 commit comments

Comments
 (0)