cn.com.crcement.oa.base.download.helper.ProgressHelper.java Source code

Java tutorial

Introduction

Here is the source code for cn.com.crcement.oa.base.download.helper.ProgressHelper.java

Source

/**
 * Copyright 2015 ZhangQu Li
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.com.crcement.oa.base.download.helper;

import java.io.IOException;

import cn.com.crcement.oa.base.download.listener.ProgressListener;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import cn.com.crcement.oa.base.download.progress.ProgressRequestBody;
import cn.com.crcement.oa.base.download.progress.ProgressResponseBody;

/**
 *  User:lizhangqu(513163535@qq.com) Date:2015-09-02 Time: 17:33
 */
public class ProgressHelper {
    /**
     * OkHttpClient
     * 
     * @param client
     *            OkHttpClient
     * @param progressListener
     *            ?
     * @return ?OkHttpClientclone
     */
    public static OkHttpClient addProgressResponseListener(OkHttpClient client,
            final ProgressListener progressListener) {
        // 
        OkHttpClient clone = client.clone();
        // 
        clone.networkInterceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                // 
                Response originalResponse = chain.proceed(chain.request());
                // ?
                return originalResponse.newBuilder()
                        .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
            }
        });
        return clone;
    }

    /**
     * 
     * 
     * @param requestBody
     *            RequestBody
     * @param progressRequestListener
     *            ?
     * @return ?
     */
    public static ProgressRequestBody addProgressRequestListener(RequestBody requestBody,
            ProgressListener progressRequestListener) {
        // 
        return new ProgressRequestBody(requestBody, progressRequestListener);
    }
}