美文网首页
HttpLoggingInterceptor 拦截 请求参数和请

HttpLoggingInterceptor 拦截 请求参数和请

作者: 鹅鹅鹅曲项向天歌呀 | 来源:发表于2019-05-10 09:18 被阅读0次
public class LoggingInterceptor implements Interceptor {
   String TAG = "mmm";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        long startTime = System.currentTimeMillis();
        Response response = chain.proceed(chain.request());
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        ResponseBody responseBody = response.body();
        if (responseBody == null) {
            return response;
        }
        okhttp3.MediaType mediaType = responseBody.contentType();
        String content = response.body().string();
        Log.e(TAG, "请求地址:  " + request.toString());
        Log.e(TAG, "请求状态:  " + response.code() + " : " + response.message());
        String method = request.method();
        if ("POST".equals(method)) {
            Buffer buffer = new Buffer();
            try {
                request.body().writeTo(buffer);
                Charset charset = Charset.forName("UTF-8");
                MediaType contentType = request.body().contentType();
                if (contentType != null) {
                    charset = contentType.charset(UTF_8);
                }
                String params = buffer.readString(charset);
                Log.e(TAG, "请求参数:  " + params);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Log.e(TAG, "请求结果:  " + content);
        Log.e(TAG, "==================================请求耗时:  " + duration + "毫秒===================================");
        return response.newBuilder().body(okhttp3.ResponseBody.create(mediaType, content)).build();

    }
}

相关文章

网友评论

      本文标题:HttpLoggingInterceptor 拦截 请求参数和请

      本文链接:https://www.haomeiwen.com/subject/ifkooqtx.html