Search in sources :

Example 1 with CallbackOk

use of com.okhttplib.callback.CallbackOk in project OkHttp3 by MrZhousf.

the class OkMainHandler method handleMessage.

@Override
public void handleMessage(Message msg) {
    final int what = msg.what;
    try {
        switch(what) {
            case //网络请求
            RESPONSE_CALLBACK:
                CallbackMessage callMsg = (CallbackMessage) msg.obj;
                if (null != callMsg.callback) {
                    //开始回调
                    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);
                            }
                        }
                    }
                }
                Call call = callMsg.call;
                if (call != null) {
                    if (!call.isCanceled()) {
                        call.cancel();
                    }
                    BaseActivityLifecycleCallbacks.cancel(callMsg.requestTag, call);
                }
                break;
            case //进度回调
            PROGRESS_CALLBACK:
                ProgressMessage proMsg = (ProgressMessage) msg.obj;
                if (null != proMsg.progressCallback) {
                    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) {
                    if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(uploadMsg.requestTag)) {
                        uploadMsg.progressCallback.onResponseMain(uploadMsg.filePath, uploadMsg.info);
                    }
                }
                break;
            case //下载结果回调
            RESPONSE_DOWNLOAD_CALLBACK:
                DownloadMessage downloadMsg = (DownloadMessage) msg.obj;
                if (null != downloadMsg) {
                    if (!BaseActivityLifecycleCallbacks.isActivityDestroyed(downloadMsg.requestTag)) {
                        downloadMsg.progressCallback.onResponseMain(downloadMsg.filePath, downloadMsg.info);
                    }
                }
                break;
            default:
                super.handleMessage(msg);
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Call(okhttp3.Call) ProgressMessage(com.okhttplib.bean.ProgressMessage) CallbackMessage(com.okhttplib.bean.CallbackMessage) CallbackOk(com.okhttplib.callback.CallbackOk) HttpInfo(com.okhttplib.HttpInfo) UploadMessage(com.okhttplib.bean.UploadMessage) DownloadMessage(com.okhttplib.bean.DownloadMessage) BaseCallback(com.okhttplib.callback.BaseCallback)

Aggregations

HttpInfo (com.okhttplib.HttpInfo)1 CallbackMessage (com.okhttplib.bean.CallbackMessage)1 DownloadMessage (com.okhttplib.bean.DownloadMessage)1 ProgressMessage (com.okhttplib.bean.ProgressMessage)1 UploadMessage (com.okhttplib.bean.UploadMessage)1 BaseCallback (com.okhttplib.callback.BaseCallback)1 CallbackOk (com.okhttplib.callback.CallbackOk)1 Call (okhttp3.Call)1