OkHttp日志拦截器打印

public class LogInterceptor implements Interceptor {
    public static String TAG = "LogInterceptor";
    @Override
    public Response intercept(Interceptor.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;
        MediaType mediaType = response.body().contentType();
        String content = response.body().string();
        Log.d(TAG,"\n");
        Log.d(TAG,"----------Start----------------");
        Log.d(TAG, "| "+request.toString());
        String method=request.method();
        if("POST".equals(method)){
            StringBuilder sb = new StringBuilder();
            if (request.body() instanceof FormBody) {
                FormBody body = (FormBody) request.body();
                for (int i = 0; i < body.size(); i++) {
                    sb.append(body.encodedName(i) + "=" + body.encodedValue(i) + ",");
                }
                sb.delete(sb.length() - 1, sb.length());
                Log.d(TAG, "| RequestParams:{"+sb.toString()+"}");
            }
        }
        Log.d(TAG, "| Response:" + content);
        Log.d(TAG,"----------End:"+duration+"毫秒----------");
        return response.newBuilder()
                .body(ResponseBody.create(mediaType, content))
                .build();
    }

}

公共参数

package com.bawie.shopmkk.utils;

import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.bawie.shopmkk.application.FrescoApplication;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author: 17826
 * created on: 2018/12/05 14:30
 * description: 自定义拦截器,用于封装公用参数
 */
public class MyInterceptor implements Interceptor {

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

        //获取原始的Request请求
        Request request = chain.request();

        SharedPreferences sharedPreferences = FrescoApplication.mContext.getSharedPreferences("kk", 0);
        String userId = sharedPreferences.getString("userId", "");
        String sessionId = sharedPreferences.getString("sessionId", "");

        //如果有一个为空,不用添加请求头
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(sessionId)) {
            return chain.proceed(request);
        }

        Request build = request.newBuilder()
                .addHeader("userId", userId)
                .addHeader("sessionId", sessionId)
                .build();
        return chain.proceed(build);

        /*//获取请求方法
        String method = request.method();

        *//* 不用method.equals("GET")的原因:
         * method有可能为null,会造成的空指针异常 *//*
        //判断请求的类型
        if ("GET".equals(method)) {
            HttpUrl httpUrl = request.url()
                    .newBuilder()
                    .addQueryParameter(PARAMETER_NAME, PARAMETER_VALUE)
                    .build();
            //获取添加公共参数之后的Request对象
            request = new Request.Builder().url(httpUrl)
                    .build();
        } else if ("POST".equals(method)) {
            RequestBody body = request.body();

            if (body instanceof FormBody) {
                FormBody.Builder builder = new FormBody.Builder();

                //获取原来Request中的参数
                FormBody originalFormBody = (FormBody)body;
                //遍历添加到新FormBody中
                for (int i = 0; i < originalFormBody.size(); i++) {
                    builder.add(originalFormBody.name(i), originalFormBody.value(i));
                }
                //添加公共参数
                builder.add(PARAMETER_NAME, PARAMETER_VALUE);
                FormBody formBody = builder.build();

                //获取添加公共参数之后的Request对象
                request = request.newBuilder()
                        .post(formBody)
                        .build();
            }
        }
        //发送拼接完后的请求
        return chain.proceed(request);*/
    }

}

发布了57 篇原创文章 · 获赞 12 · 访问量 8378
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览