Search in sources :

Example 1 with ProgressMessage

use of com.okhttplib.bean.ProgressMessage 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 ProgressMessage

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

the class ProgressRequestBody method sink.

private Sink sink(Sink originalSink) {
    return new ForwardingSink(originalSink) {

        long bytesWritten = 0L;

        long contentLength = 0L;

        int lastPercent = 0;

        @Override
        public void write(Buffer source, long byteCount) throws IOException {
            super.write(source, byteCount);
            if (contentLength == 0) {
                contentLength = contentLength();
            }
            bytesWritten += byteCount;
            if (null != progressCallback) {
                int percent = (int) ((100 * bytesWritten) / contentLength);
                // 每处理1%则立即回调
                if (percent != lastPercent) {
                    lastPercent = percent;
                    progressCallback.onProgressAsync(percent, bytesWritten, contentLength, bytesWritten == contentLength);
                    // 主线程回调
                    Message msg = new ProgressMessage(OkMainHandler.PROGRESS_CALLBACK, progressCallback, percent, bytesWritten, contentLength, bytesWritten == contentLength, requestTag).build();
                    OkMainHandler.getInstance().sendMessage(msg);
                }
            }
        }
    };
}
Also used : Buffer(okio.Buffer) ProgressMessage(com.okhttplib.bean.ProgressMessage) Message(android.os.Message) ProgressMessage(com.okhttplib.bean.ProgressMessage) ForwardingSink(okio.ForwardingSink)

Example 3 with ProgressMessage

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

the class ProgressResponseBody method source.

private Source source(Source source) {
    return new ForwardingSource(source) {

        long totalBytesRead = 0L;

        long contentLength = 0L;

        ProgressCallback progressCallback;

        int lastPercent = 0;

        @Override
        public long read(Buffer sink, long byteCount) throws IOException {
            long bytesRead = super.read(sink, byteCount);
            if (totalBytesRead == 0) {
                totalBytesRead = downloadFileInfo.getCompletedSize();
                Log.d(requestTag + "[" + timeStamp + "]", "从节点[" + totalBytesRead + "]开始下载" + downloadFileInfo.getSaveFileNameWithExtension());
            }
            if (contentLength == 0) {
                // 文件总长度=当前需要下载长度+已完成长度
                contentLength = contentLength() + totalBytesRead;
            }
            totalBytesRead += bytesRead != -1 ? bytesRead : 0;
            if (null == progressCallback)
                progressCallback = downloadFileInfo.getProgressCallback();
            if (null != progressCallback) {
                int percent = (int) ((100 * totalBytesRead) / contentLength);
                // 每处理1%则立即回调
                if (percent != lastPercent) {
                    lastPercent = percent;
                    progressCallback.onProgressAsync(percent, totalBytesRead, contentLength, totalBytesRead == -1);
                    // 主线程回调
                    Message msg = new ProgressMessage(OkMainHandler.PROGRESS_CALLBACK, progressCallback, percent, totalBytesRead, contentLength, bytesRead == -1, requestTag).build();
                    OkMainHandler.getInstance().sendMessage(msg);
                }
            }
            return bytesRead;
        }
    };
}
Also used : Buffer(okio.Buffer) ProgressMessage(com.okhttplib.bean.ProgressMessage) Message(android.os.Message) ProgressMessage(com.okhttplib.bean.ProgressMessage) ProgressCallback(com.okhttplib.callback.ProgressCallback) ForwardingSource(okio.ForwardingSource)

Aggregations

ProgressMessage (com.okhttplib.bean.ProgressMessage)3 Message (android.os.Message)2 Buffer (okio.Buffer)2 HttpInfo (com.okhttplib.HttpInfo)1 CallbackMessage (com.okhttplib.bean.CallbackMessage)1 DownloadMessage (com.okhttplib.bean.DownloadMessage)1 UploadMessage (com.okhttplib.bean.UploadMessage)1 BaseCallback (com.okhttplib.callback.BaseCallback)1 CallbackOk (com.okhttplib.callback.CallbackOk)1 ProgressCallback (com.okhttplib.callback.ProgressCallback)1 Call (okhttp3.Call)1 Response (okhttp3.Response)1 ForwardingSink (okio.ForwardingSink)1 ForwardingSource (okio.ForwardingSource)1