OkHttpファイルのダウンロードとInterceptorによるダウンロードの進捗状況


OkHttp Interceptorを通じて書き換え要求と応答を実現することができて、機能はとても強くて、ログをブロックすることができるだけではなくて、ダウンロードファイルの進度を傍受することができて、この点はとても重要で、ここで私は1つのテストクラスを書いてみんなと分かち合います:
public class ProgressDownLoadFile {

    private static final OkHttpClient client = new OkHttpClient();

    public static void main(String[] args) throws IOException {

        // 
        final ProgressListener progressListener = new ProgressListener() {
            @Override public void update(long bytesRead, long contentLength, boolean done) {
                System.out.format("%d%% done
"
, (100 * bytesRead) / contentLength); } }; // , ResponseBody, client.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(); } }); // Request request = new Request.Builder() // .url("http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1308/15/c2/24494083_1376530583817.jpg") .build(); // client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { System.out.println("failure"); } @Override public void onResponse(Response response) throws IOException { // , int len; byte[] buf = new byte[2048]; InputStream inputStream = response.body().byteStream(); // File file1 = new File("d:\\file.jpg"); FileOutputStream fileOutputStream = new FileOutputStream(file1); while ((len = inputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, len); } fileOutputStream.flush(); fileOutputStream.close(); inputStream.close(); } }); } /** * ResponseBody */ private static class ProgressResponseBody extends ResponseBody { private final ResponseBody responseBody; private final ProgressListener progressListener; private BufferedSource bufferedSource; public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) { this.responseBody = responseBody; this.progressListener = progressListener; } @Override public MediaType contentType() { return responseBody.contentType(); } @Override public long contentLength() throws IOException { return responseBody.contentLength(); } @Override public BufferedSource source() throws IOException { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); } return bufferedSource; } private Source source(Source source) { return new ForwardingSource(source) { long totalBytesRead = 0L; @Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead = super.read(sink, byteCount); totalBytesRead += bytesRead != -1 ? bytesRead : 0; progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1); return bytesRead; } }; } } interface ProgressListener { /** * @param bytesRead * @param contentLength * @param done */ void update(long bytesRead, long contentLength, boolean done); } }

後でファイルアップロードの進捗を検討しますので、ご期待ください.