use of com.okhttplib.bean.DownloadMessage in project OkHttp3 by MrZhousf.
the class HttpHelper method responseCallback.
/**
* 请求结果回调
*/
void responseCallback(HttpInfo info, ProgressCallback progressCallback, int code, String requestTag) {
//同步回调
if (null != progressCallback)
progressCallback.onResponseSync(info.getUrl(), info);
//异步主线程回调
if (OkMainHandler.RESPONSE_DOWNLOAD_CALLBACK == code) {
Message msg = new DownloadMessage(code, info.getUrl(), info, progressCallback, requestTag).build();
OkMainHandler.getInstance().sendMessage(msg);
} else if (OkMainHandler.RESPONSE_UPLOAD_CALLBACK == code) {
Message msg = new UploadMessage(code, info.getUrl(), info, progressCallback, requestTag).build();
OkMainHandler.getInstance().sendMessage(msg);
}
}
use of com.okhttplib.bean.DownloadMessage in project OkHttp3 by MrZhousf.
the class OkMainHandler method handleMessage.
@Override
public void handleMessage(Message msg) {
final int what = msg.what;
String requestTag = "";
try {
switch(what) {
case // 网络请求
RESPONSE_CALLBACK:
CallbackMessage callMsg = (CallbackMessage) msg.obj;
if (null != callMsg.callback) {
// 开始回调
requestTag = callMsg.requestTag;
if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(callMsg.requestTag)) {
BaseCallback callback = callMsg.callback;
if (callback instanceof CallbackOk) {
((CallbackOk) callback).onResponse(callMsg.info);
} else if (callback instanceof com.okhttplib.callback.Callback) {
HttpInfo info = callMsg.info;
if (info.isSuccessful()) {
((com.okhttplib.callback.Callback) callback).onSuccess(info);
} else {
((com.okhttplib.callback.Callback) callback).onFailure(info);
}
// 当返回结果是Response时自动关闭ResponseBody
if (info.isNeedResponse()) {
Response res = info.getResponse();
if (null != res) {
res.close();
}
}
}
}
}
Call call = callMsg.call;
if (call != null) {
if (!call.isCanceled()) {
call.cancel();
}
BaseActivityLifecycleCallbacks.cancel(requestTag, call);
}
break;
case // 进度回调
PROGRESS_CALLBACK:
ProgressMessage proMsg = (ProgressMessage) msg.obj;
if (null != proMsg.progressCallback) {
requestTag = proMsg.requestTag;
if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(proMsg.requestTag)) {
proMsg.progressCallback.onProgressMain(proMsg.percent, proMsg.bytesWritten, proMsg.contentLength, proMsg.done);
}
}
break;
case // 上传结果回调
RESPONSE_UPLOAD_CALLBACK:
UploadMessage uploadMsg = (UploadMessage) msg.obj;
if (null != uploadMsg.progressCallback) {
requestTag = uploadMsg.requestTag;
if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(requestTag)) {
uploadMsg.progressCallback.onResponseMain(uploadMsg.filePath, uploadMsg.info);
BaseActivityLifecycleCallbacks.cancel(requestTag);
}
}
break;
case // 下载结果回调
RESPONSE_DOWNLOAD_CALLBACK:
DownloadMessage downloadMsg = (DownloadMessage) msg.obj;
if (null != downloadMsg) {
requestTag = downloadMsg.requestTag;
if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(requestTag)) {
downloadMsg.progressCallback.onResponseMain(downloadMsg.filePath, downloadMsg.info);
BaseActivityLifecycleCallbacks.cancel(requestTag);
}
}
break;
default:
super.handleMessage(msg);
break;
}
} catch (Exception e) {
BaseActivityLifecycleCallbacks.cancel(requestTag);
}
}
use of com.okhttplib.bean.DownloadMessage in project OkHttp3 by MrZhousf.
the class BaseHelper method responseCallback.
/**
* 请求结果回调
*/
void responseCallback(HttpInfo info, ProgressCallback progressCallback, int code, String requestTag) {
// 同步回调
if (null != progressCallback)
progressCallback.onResponseSync(info.getUrl(), info);
// 异步主线程回调
if (OkMainHandler.RESPONSE_DOWNLOAD_CALLBACK == code) {
Message msg = new DownloadMessage(code, info.getUrl(), info, progressCallback, requestTag).build();
OkMainHandler.getInstance().sendMessage(msg);
} else if (OkMainHandler.RESPONSE_UPLOAD_CALLBACK == code) {
Message msg = new UploadMessage(code, info.getUrl(), info, progressCallback, requestTag).build();
OkMainHandler.getInstance().sendMessage(msg);
}
}
Aggregations