java 用于OkHttp和Retrofit的GZip日志拦截器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 用于OkHttp和Retrofit的GZip日志拦截器相关的知识,希望对你有一定的参考价值。

package myapp;

import java.io.IOException;
import java.nio.charset.Charset;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.platform.Platform;
import okio.Buffer;
import okio.BufferedSource;
import okio.GzipSource;

import static okhttp3.internal.platform.Platform.INFO;


public final class GZipLoggingInterceptor implements Interceptor {
    private static final Charset UTF8 = Charset.forName("UTF-8");
    
    public interface Logger {
        void log(String message);

        Logger DEFAULT = new Logger() {
            @Override public void log(String message) {
                Platform.get().log(INFO, message, null);
            }
        };
    }

    public GZipLoggingInterceptor() {
        this(Logger.DEFAULT);
    }

    public GZipLoggingInterceptor(Logger logger) {
        this.logger = logger;
    }

    private final Logger logger;

    @Override public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();

        Response response;
        try {
            response = chain.proceed(request);
        } catch (Exception e) {
            logger.log("<-- HTTP FAILED: " + e);
            throw e;
        }

        ResponseBody responseBody = response.body();
        long contentLength = responseBody.contentLength();
        Headers headers = response.headers();


        BufferedSource source = responseBody.source();
        source.request(Long.MAX_VALUE); // Buffer the entire body.
        Buffer buffer = source.buffer();

        if ("gzip".equalsIgnoreCase(headers.get("Content-Encoding"))) {
            GzipSource gzippedResponseBody = null;
            try {
                gzippedResponseBody = new GzipSource(buffer.clone());
                buffer = new Buffer();
                buffer.writeAll(gzippedResponseBody);
            } finally {
                if (gzippedResponseBody != null) {
                    gzippedResponseBody.close();
                }
            }

            Charset charset = UTF8;
            MediaType contentType = responseBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }

            if ((contentLength != 0)&&(buffer!=null)) {
                logger.log("");
                String bufferString = buffer.clone().readString(charset);
                logger.log("<-- BEGIN GZIP CONTENT (" + bufferString.length() + "-byte)");
                logger.log(bufferString);
                logger.log("<-- END GZIP CONTENT (" +  bufferString.length() + "-byte)");
            }
        }

        return response;
    }
}
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.addNetworkInterceptor(new GZipLoggingInterceptor());

以上是关于java 用于OkHttp和Retrofit的GZip日志拦截器的主要内容,如果未能解决你的问题,请参考以下文章

OKHttp Authenticator 不适用于 Retrofit 暂停乐趣

「2020 新手必备 」极速入门 Retrofit + OkHttp 网络框架到实战,这一篇就够了!

java 使用Retrofit / OkHttp轻松处理Cookies

使用 Retrofit、okhttp 和 picasso 缓存图像和字符串

证书固定不适用于 Android 上的 OkHttp

java 使用Retrofit 1.9 + OkHttp 2.2进行Android响应缓存