OkHttp学习(2)-->>异步下载图片、文件(拦截器重写Response方法实现下载进度获取)

OkHttp学习(1)–>>同步和异步(get、post键值对、post带map、请求头体封装json)

Volley学习链接—想了解Volley的可以看我这5篇

今天来了解下okHttp如何进行图片、文件下载的
首先在OkHttpManger弄一个单利模式,初始化一个OkHttpClient,然后放进去一个handler,线程更新ui使用

private Handler okHttpHandler;
    private OkHttpClient mOkHttpClient;
    private OkHttpManger(){
        this.mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)//连接超时限制
                .writeTimeout(30, TimeUnit.SECONDS)//写入超时
                .readTimeout(30, TimeUnit.SECONDS)//读取超时
                .build();
        this.okHttpHandler = new Handler(Looper.getMainLooper());
    }

    public static final OkHttpManger getInstance(){
        return SingleFactory.manger;
    }
    private static final class SingleFactory{
        private static final OkHttpManger manger = new OkHttpManger();
    }

然后直接执行下载方法

 public void downloadAsync(final String url, final String destFileDir,
                              final OKHttpUICallback.ProgressCallback downListener) throws IOException {
        File file = new File(destFileDir,getFileName(url));
        if(!file.exists()){
            file.createNewFile();
        }
        downloadAsync(url, file, downListener);
    }

参数1为图片的url,我们这里用了http://img.mukewang.com/55249cf30001ae8a06000338.jpg来测试,
参数2destFileDir这个是下载完毕保存到手机的目录地址,我们传入的参数如下:

Config.getDirFile("download").getAbsolutePath()

这里做了一些目录的设置

public static File getDirFile(String subFileName){
        if(isSDMounted()){
            return mkdirsFolder(getSDirAbsolutePath() + "/Seeker/" + subFileName);
        }
        return null;
    }

    public static boolean isSDMounted(){
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static File mkdirsFolder(String path){
        File file = new File(path);
        if(!file.exists()){
            return file.mkdirs()?file:null;
        }
        return file;
    }

    public static String getSDirAbsolutePath(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

就是在sd卡根目录下创建Seeker/download文件夹,下载的图片就存在download文件夹下
目录有了,下载完毕图片,我们需要起一个名字把:

 File file = new File(destFileDir,getFileName(url));
        if(!file.exists()){
            file.createNewFile();
        }
 private String getFileName(String url){
        int lastSeparaorIndex = url.lastIndexOf("/");
        return (lastSeparaorIndex < 0) ? url : url.substring(lastSeparaorIndex + 1, url.length());
    }

然后正式进入下载的代码

public void downloadAsync(final String url, final File downFile,
                              final OKHttpUICallback.ProgressCallback downListener) throws IOException {
        OkHttpClient downLoadClient = mOkHttpClient.newBuilder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        //拦截
                        Response origin = chain.proceed(chain.request());
                        //包装响应体
                        return origin.newBuilder()
                                .body(new ProgressBody.ProgressResponseBody(origin.body(), downListener, okHttpHandler))
                                .build();
                    }
                })
                .build();
        final Request request = new Request.Builder().url(url).build();
        downLoadClient.newCall(request).enqueue(new OKHttpThreadCallback(okHttpHandler,downListener,true).setFile(downFile));
    }

我们来看下代码里面做了什么操作?我们首先想到的是弄一个接口把
OKHttpUICallback.ProgressCallback downListener接口有3个接口方法,意思很明显。

 public interface ProgressCallback{
        void onSuccess(Call call, Response response, String path);
        void onProgress(long byteReadOrWrite, long contentLength, boolean done);
        void onError(Call call, IOException e);
    }

okhttp实现了拦截器Interceptor,这个是什么意思呢?

Interceptors area powerful mechanism that can monitor, rewrite, and retry calls.
拦截器可以用来转换,重试,重写请求的机制。这是官方的定义。
一句话就是对请求、相应加上自己的代码,重写方法

OkHttpClient downLoadClient = mOkHttpClient.newBuilder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        //拦截
                        Response origin = chain.proceed(chain.request());
                        //包装响应体
                        return origin.newBuilder()
                                .body(new ProgressBody.ProgressResponseBody(origin.body(), downListener, okHttpHandler))
                                .build();
                    }
                })
                .build();

这里就是用到了拦截器,大概意思就是,对请求响应体,进行二次包装,比如请求图片、文件的进度问题
我们看下我们做了什么操作呢?

new ProgressBody.ProgressResponseBody(origin.body(), downListener, okHttpHandler)
  @Override
        public BufferedSource source() {
            if(bufferedSource == null){
                bufferedSource = Okio.buffer(source(responseBody.source()));
            }
            return bufferedSource;
        }

        /**
         * 读取,回调进度接口
         * @return
         */
        private Source source(Source source){
            return new ForwardingSource(source) {
                //读取当前获取的字节数
                long totalBytesRead = 0L;

                @Override
                public long read(Buffer sink, long byteCount) throws IOException {
                    final long byteRead =  super.read(sink, byteCount);
                    if(mHandler != null && mListener != null){
                        //增加当前读取的字节数,如果读取完成则返回-1
                        totalBytesRead += byteRead != -1?byteRead:0;
                        //回调,若是contentLength()不知道长度,则返回-1
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mListener.onProgress(totalBytesRead, contentLength(), byteRead == -1);
                            }
                        });
                    }
                    return byteRead;
                }
            };
        }

mListener.onProgress(totalBytesRead, contentLength(), byteRead == -1);就是对请求进度的一个对外通讯,告诉ui界面可以做一些操作,比如进度条之类的

然后继续看那个下载的入口方法里面的

downLoadClient.newCall(request).enqueue(new OKHttpThreadCallback(okHttpHandler,downListener,true).setFile(downFile));

这里面做了什么操作呢?

 @Override
    public void onFailure(final Call call, final IOException e) {
        if(UICallback != null && UIHandler != null){
            UIHandler.post(new Runnable() {
                @Override
                public void run() {
                    UICallback.onError(call,e);
                }
            });
        }
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if(isDownload){
            download(call,response);
        }else{
            postSuccess(call,response);
        }
    }

就是在请求相应里面,进行sd写入

private void download(Call call, Response response) throws IOException {
        if(downFile == null){
            throw new NullPointerException("downFile == null");
        }
        byte[] buffer = new byte[2048];
        InputStream is = response.body().byteStream();
        int len;
        FileOutputStream fos = new FileOutputStream(downFile);
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fos.flush();
        if(is != null){
            is.close();
        }
        if (fos != null){
            fos.close();
        }
        postSuccess(call,null);
    }

然后在主界面进行接口方法实现,去实现其他需要处理的

 UICallback.onSuccess(call, response,downFile == null?null:downFile.getAbsolutePath());

如下是下载过程的log

09-01 18:05:55.398 22636-22636/com.example.administrator.myapplication I/MainActivity: downloadAsync
09-01 18:05:55.826 22636-22636/com.example.administrator.myapplication I/MainActivity: byteReadOrWrite:798,contentLength:471776,done:false
................
...............
09-01 18:06:04.795 22636-22636/com.example.administrator.myapplication I/MainActivity: byteReadOrWrite:471776,contentLength:471776,done:true
09-01 18:06:04.797 22636-22636/com.example.administrator.myapplication I/MainActivity: path:/storage/emulated/0/Seeker/download/55249cf30001ae8a06000338.jpg

另外附下载截图一张:
这里写图片描述

//以下是代码堆积区//

MainActivity

package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button downloadAsync;
    ImageView imageView;
    String url = "http://img.mukewang.com/55249cf30001ae8a06000338.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imageView = (ImageView) findViewById(R.id.imageView);
        downloadAsync = (Button) findViewById(R.id.downloadAsync);
        downloadAsync.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.downloadAsync:
                Log.i("MainActivity","downloadAsync");
                //下载图片
                try {
                    OkHttpManger.getInstance().downloadAsync(url, Config.getDirFile("download").getAbsolutePath(), new OKHttpUICallback.ProgressCallback() {
                        @Override
                        public void onSuccess(Call call, Response response, String path) {
                            Log.i("MainActivity","path:"+path);
                        }

                        @Override
                        public void onProgress(long byteReadOrWrite, long contentLength, boolean done) {
                            Log.i("MainActivity","byteReadOrWrite:"+byteReadOrWrite+",contentLength:"+contentLength+",done:"+done);
                        }

                        @Override
                        public void onError(Call call, IOException e) {
                            Log.i("MainActivity","onError");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

        }

    }
}

OkHttpManger

package com.example.administrator.myapplication;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * OkHttp 工具类,
 * get的同步异步请求
 * post的json字符串同步异步上传
 * post的键值对同步异步上传
 * post文件异步上传,回调结果以及进度
 * 异步下载文件,回调结果以及进度
 *
 * Created by Seeker on 2016/6/24.
 */
public final class OkHttpManger {

    private static final String TAG = "OkHttpManger";

    private static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");

    private Handler okHttpHandler;
    private OkHttpClient mOkHttpClient;
    private OkHttpManger(){
        this.mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)//连接超时限制
                .writeTimeout(30, TimeUnit.SECONDS)//写入超时
                .readTimeout(30, TimeUnit.SECONDS)//读取超时
                .build();
        this.okHttpHandler = new Handler(Looper.getMainLooper());
    }

    public static final OkHttpManger getInstance(){
        return SingleFactory.manger;
    }
    private static final class SingleFactory{
        private static final OkHttpManger manger = new OkHttpManger();
    }


    /**
     * 异步下载文件,实现了下载进度的提示
     *
     * @param url
     * @param destFileDir 文件存储根路径
     * @param downListener
     */
    public void downloadAsync(final String url, final String destFileDir,
                              final OKHttpUICallback.ProgressCallback downListener) throws IOException {
        File file = new File(destFileDir,getFileName(url));
        if(!file.exists()){
            file.createNewFile();
        }
        downloadAsync(url, file, downListener);
    }
    /**
     * 获取文件名
     *
     * @param path
     */
    private String getFileName(String url){
        int lastSeparaorIndex = url.lastIndexOf("/");
        return (lastSeparaorIndex < 0) ? url : url.substring(lastSeparaorIndex + 1, url.length());
    }
    /**
     * 异步下载文件,实现了下载进度的提示
     * @param url
     * @param downFile:存储文件(文件存储绝对路径文件)
     * @param downListener
     * @throws IOException
     */
    public void downloadAsync(final String url, final File downFile,
                              final OKHttpUICallback.ProgressCallback downListener) throws IOException {
        OkHttpClient downLoadClient = mOkHttpClient.newBuilder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        //拦截
                        Response origin = chain.proceed(chain.request());
                        //包装响应体
                        return origin.newBuilder()
                                .body(new ProgressBody.ProgressResponseBody(origin.body(), downListener, okHttpHandler))
                                .build();
                    }
                })
                .build();
        final Request request = new Request.Builder().url(url).build();
        downLoadClient.newCall(request).enqueue(new OKHttpThreadCallback(okHttpHandler,downListener,true).setFile(downFile));
    }

}

ProgressBody

package com.example.administrator.myapplication;

import android.os.Handler;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ForwardingSink;
import okio.ForwardingSource;
import okio.Okio;
import okio.Sink;
import okio.Source;

/**
 * Created by Safly on 2016/9/1.
 */
public final class ProgressBody {

    /**
     * 包装响应体,用于处理提示下载进度
     *
     * Created by Seeker on 2016/6/29.
     */
    public static final class ProgressResponseBody extends ResponseBody {

        //实际待包装的响应体
        private final ResponseBody responseBody;

        //进度回调接口
        private OKHttpUICallback.ProgressCallback mListener;

        //包装完成的BufferedSource
        private BufferedSource bufferedSource;

        //传递下载进度到主线程
        private Handler mHandler;

        public ProgressResponseBody(ResponseBody responseBody, OKHttpUICallback.ProgressCallback listener, Handler handler){
            this.responseBody = responseBody;
            this.mListener = listener;
            this.mHandler = handler;
        }

        @Override
        public MediaType contentType() {
            return responseBody.contentType();
        }

        @Override
        public long contentLength() {
            return responseBody.contentLength();
        }

        @Override
        public BufferedSource source() {
            if(bufferedSource == null){
                bufferedSource = Okio.buffer(source(responseBody.source()));
            }
            return bufferedSource;
        }

        /**
         * 读取,回调进度接口
         * @return
         */
        private Source source(Source source){
            return new ForwardingSource(source) {
                //读取当前获取的字节数
                long totalBytesRead = 0L;

                @Override
                public long read(Buffer sink, long byteCount) throws IOException {
                    final long byteRead =  super.read(sink, byteCount);
                    if(mHandler != null && mListener != null){
                        //增加当前读取的字节数,如果读取完成则返回-1
                        totalBytesRead += byteRead != -1?byteRead:0;
                        //回调,若是contentLength()不知道长度,则返回-1
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                mListener.onProgress(totalBytesRead, contentLength(), byteRead == -1);
                            }
                        });
                    }
                    return byteRead;
                }
            };
        }
    }

}

OKHttpThreadCallback

package com.example.administrator.myapplication;

import android.os.Handler;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public final class OKHttpThreadCallback implements Callback {

    private Handler UIHandler;

    private OKHttpUICallback.ProgressCallback UICallback;

    private boolean isDownload;

    private File downFile;

    public OKHttpThreadCallback(Handler handler, OKHttpUICallback.ProgressCallback callback, boolean isDownload){
        this.UIHandler = handler;
        this.UICallback = callback;
        this.isDownload = isDownload;
    }

    @Override
    public void onFailure(final Call call, final IOException e) {
        if(UICallback != null && UIHandler != null){
            UIHandler.post(new Runnable() {
                @Override
                public void run() {
                    UICallback.onError(call,e);
                }
            });
        }
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if(isDownload){
            download(call,response);
        }else{
            postSuccess(call,response);
        }
    }

    /**
     * 设置保存file
     * @param file
     */
    public OKHttpThreadCallback setFile(File file){
        this.downFile = file;
        return this;
    }

    /**
     * 获取下载数据并写入文件
     * @param response
     */
    private void download(Call call, Response response) throws IOException {
        if(downFile == null){
            throw new NullPointerException("downFile == null");
        }
        byte[] buffer = new byte[2048];
        InputStream is = response.body().byteStream();
        int len;
        FileOutputStream fos = new FileOutputStream(downFile);
        while ((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fos.flush();
        if(is != null){
            is.close();
        }
        if (fos != null){
            fos.close();
        }
        postSuccess(call,null);
    }

    /**
     * 回调成功信息
     * @param call
     * @param response
     */
    private void postSuccess(final Call call, final Response response){
        if(UICallback != null && UIHandler != null){
            UIHandler.post(new Runnable() {
                @Override
                public void run() {
                    UICallback.onSuccess(call, response,downFile == null?null:downFile.getAbsolutePath());
                }
            });
        }
    }
}

Config

package com.example.administrator.myapplication;


import android.os.Environment;

import java.io.File;


public class Config {


    public static File getDirFile(String subFileName){
        if(isSDMounted()){
            return mkdirsFolder(getSDirAbsolutePath() + "/Seeker/" + subFileName);
        }
        return null;
    }

    public static boolean isSDMounted(){
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static File mkdirsFolder(String path){
        File file = new File(path);
        if(!file.exists()){
            return file.mkdirs()?file:null;
        }
        return file;
    }

    public static String getSDirAbsolutePath(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

}

最后看一个接口

package com.example.administrator.myapplication;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by safly on 2016/9/1.
 * 回调主线程的接口
 */
public class OKHttpUICallback {
    /**
     * 带有进度的上传、下载回调接口
     */
    public interface ProgressCallback{
        void onSuccess(Call call, Response response, String path);
        void onProgress(long byteReadOrWrite, long contentLength, boolean done);
        void onError(Call call, IOException e);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值