OKHttp上传图片实现,进度监听

1.为了实现图片进度监听,需要定义一个类,继承RequestBody

/**
 * Created by krubo on 2016/2/17.
 */
public abstract class CmlRequestBody extends RequestBody {

    private final RequestBody requestBody;
    private BufferedSink bufferedSink;

    public CmlRequestBody(RequestBody requestBody) {
        this.requestBody = requestBody;
    }


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

    @Override
    public long contentLength() throws IOException {
        return requestBody.contentLength();
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        if (bufferedSink == null) {
            bufferedSink = Okio.buffer(sink(sink));
        }
        requestBody.writeTo(bufferedSink);
        //必须调用flush,否则最后一部分数据可能不会被写入
        bufferedSink.flush();
    }

    private Sink sink(Sink sink) {
        return new ForwardingSink(sink) {
            private long current;
            private long total;
            private int last = 0;

            @Override
            public void write(Buffer source, long byteCount) throws IOException {
                super.write(source, byteCount);
                if (total == 0) {
                    total = contentLength();
                }
                current += byteCount;
                int now = (int) (current * 100 / total);
                if (last < now) {
                    loading(last, 100, total == current);
                    last = now;
                }
            }
        };
    }

    public abstract void loading(long current, long total, boolean done);
}

2.创建请求request

MultipartBody.Builder builder= new MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addFormDataPart("thumb", file.getName(),
                RequestBody.create(MediaType.parse("image/*"), file));
//添加其他参数
//builder.addFormDataPart(key, value);
Request.Builder request = new Request.Builder().url(url)
                .post(new CmlRequestBody(builder.build()) {
                    @Override
                    public void loading(long current, long total, boolean done) {
                        LogUtils.d("current:" + current + ";total:" + total);
                        if (!done) {
                            sendMessage(3, current, total, "");
                        }
                    }
                });

3.发起请求,上传图片

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .build();
okHttpClient.newCall(request.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                sendMessage(1, 0, 0, request.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (!response.isSuccessful() || response == null || response.body() == null) {
                    sendMessage(1, 0, 0, "");
                } else {
                    //返回值处理
                    ...
                    sendMessage(2, 0, 0, entity);
                }
            }
        });

4.通知主线程进行相应的处理

public void sendMessage(int what, long current, long total, String msg) {
        Message message = Message.obtain();
        message.what = what;
        message.arg1 = (int) current;
        message.arg2 = (int) total;
        message.obj = msg;
        handler.sendMessage(message);
    }
     private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    //上传失败
                    ...
                    break;
                case 2:
                    //上传成功
                    ...
                    break;
                case 3:
                    //上传进度
                    int current = msg.arg1;
                    int total = msg.arg2;
                    ...
                    break;
            }
        }
    };

你可能感兴趣的:(Android,杂项)