Android api Chrome调试拦截

添加依赖

api 'com.facebook.stetho:stetho:1.5.1'
api 'com.facebook.stetho:stetho-okhttp3:1.5.1'

在application初始化

 Stetho.initialize(Stetho.newInitializerBuilder(this)
                    .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                    .build());

添加okHttp拦截

builder.addNetworkInterceptor(new StethoInterceptorDecrypt());

重写拦截器对gzip解码

 BufferedSource source = response.body().source();
            source.request(Long.MAX_VALUE);
            Buffer buffer = source.getBuffer();
            Headers headers = response.headers();
            //对gzip编码的进行解码处理,不然获取会出现乱码
            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();
                    }
                }

            }

            String bodyStr = buffer.clone().readString(StandardCharsets.UTF_8);

后续解析会出现异常 java.io.ioexception id1id2 actual 0x00001fef = expected 0x00001f8b 添加identity头部

  response = response.newBuilder().header("Content-Encoding", "identity")
                        .body(new ForwardingResponseBody(body, responseStream))
                        .build();

你可能感兴趣的:(Android api Chrome调试拦截)