public abstract class HttpCallBack<T> implements Callback {
public abstract void onSuccess(String data,T body); public abstract void onFailure(String data); Handler mainHandler = new Handler(MyApp.getContext().getMainLooper()); @Override public void onResponse(Call call, Response response) throws IOException { if(response != null && response.isSuccessful()){ String a=response.body().string(); Class<?> c = getClass(); Type type = c.getGenericSuperclass(); type = ((ParameterizedType) type).getActualTypeArguments()[0]; String res = new String(a.getBytes()); final T body = JSON.parseObject(res, type);//JsonParseUtils.fromJson(response, type); mainHandler.post(new Runnable() { @Override public void run() { onSuccess(null, body); } }); }else{ final String body = response.body().string(); XLog.i("body:"+body); mainHandler.post(new Runnable() { @Override public void run() { onFailure(body); } }); } } }
}
}
本文深入探讨了HTTP回调抽象类的实现细节,包括泛型参数的使用、响应处理及失败情况下的异常处理策略。通过JSON解析,实现了异步请求的成功与失败回调。
6248

被折叠的 条评论
为什么被折叠?



