Search in sources :

Example 1 with CallbackMessage

use of com.okhttplib.bean.CallbackMessage 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);
    }
}
Also used : Call(okhttp3.Call) ProgressMessage(com.okhttplib.bean.ProgressMessage) CallbackMessage(com.okhttplib.bean.CallbackMessage) CallbackOk(com.okhttplib.callback.CallbackOk) HttpInfo(com.okhttplib.HttpInfo) Response(okhttp3.Response) UploadMessage(com.okhttplib.bean.UploadMessage) DownloadMessage(com.okhttplib.bean.DownloadMessage) BaseCallback(com.okhttplib.callback.BaseCallback)

Example 2 with CallbackMessage

use of com.okhttplib.bean.CallbackMessage in project OkHttp3 by MrZhousf.

the class HttpHelper method doRequestAsync.

/**
 * 异步请求
 */
void doRequestAsync(final OkHttpHelper helper) {
    if (httpInfo == null)
        return;
    final HttpInfo info = httpInfo;
    final BaseCallback callback = helper.getCallback();
    Request request = helper.getRequest();
    String url = info.getUrl();
    if (!checkUrl(url)) {
        // 主线程回调
        Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, retInfo(info, HttpInfo.CheckURL), requestTag, null).build();
        OkMainHandler.getInstance().sendMessage(msg);
        return;
    }
    request = request == null ? buildRequest(info, helper.getRequestType(), helper.getProgressCallback()) : request;
    showUrlLog(request);
    Call call = httpClient.newCall(request);
    BaseActivityLifecycleCallbacks.putCall(requestTag, call);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            // 主线程回调
            int code = HttpInfo.CheckNet;
            if (e instanceof UnknownHostException) {
                if (!helperInfo.getOkHttpUtil().isNetworkAvailable()) {
                    code = HttpInfo.CheckNet;
                } else {
                    code = HttpInfo.CheckURL;
                }
            } else if (e instanceof SocketTimeoutException) {
                code = HttpInfo.ConnectionTimeOut;
                if (null != e.getMessage()) {
                    if (e.getMessage().contains("failed to connect to"))
                        code = HttpInfo.ConnectionTimeOut;
                    if (e.getMessage().equals("timeout"))
                        code = HttpInfo.WriteAndReadTimeOut;
                }
            }
            Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, retInfo(info, code, "[" + e.getMessage() + "]"), requestTag, call).build();
            OkMainHandler.getInstance().sendMessage(msg);
        }

        @Override
        public void onResponse(Call call, Response res) throws IOException {
            // 主线程回调
            Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, dealResponse(helper, res), requestTag, call).build();
            OkMainHandler.getInstance().sendMessage(msg);
        }
    });
}
Also used : Call(okhttp3.Call) CallbackMessage(com.okhttplib.bean.CallbackMessage) Message(android.os.Message) UnknownHostException(java.net.UnknownHostException) CallbackMessage(com.okhttplib.bean.CallbackMessage) Request(okhttp3.Request) IOException(java.io.IOException) HttpInfo(com.okhttplib.HttpInfo) Response(okhttp3.Response) ProgressCallback(com.okhttplib.callback.ProgressCallback) Callback(okhttp3.Callback) BaseCallback(com.okhttplib.callback.BaseCallback) SocketTimeoutException(java.net.SocketTimeoutException) BaseCallback(com.okhttplib.callback.BaseCallback)

Aggregations

HttpInfo (com.okhttplib.HttpInfo)2 CallbackMessage (com.okhttplib.bean.CallbackMessage)2 BaseCallback (com.okhttplib.callback.BaseCallback)2 Call (okhttp3.Call)2 Response (okhttp3.Response)2 Message (android.os.Message)1 DownloadMessage (com.okhttplib.bean.DownloadMessage)1 ProgressMessage (com.okhttplib.bean.ProgressMessage)1 UploadMessage (com.okhttplib.bean.UploadMessage)1 CallbackOk (com.okhttplib.callback.CallbackOk)1 ProgressCallback (com.okhttplib.callback.ProgressCallback)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 Callback (okhttp3.Callback)1 Request (okhttp3.Request)1