LoggingInterceptor.java:输出日志
public class LoggingInterceptor implements Interceptor {
private static final String TAG = "LoggingInterceptor";
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request request = chain.request();
Log.e(TAG, "请求: " + "\n"
+ "url------" + request.url() + "\n"
+ "请求方法------" + request.method() + "\n"
+ "请求头------" + request.headers()
);
Response response = chain.proceed(request);
ResponseBody responseBody =response.peekBody(1024 *1024);
Log.e(TAG, "响应: " + "\n"
+ "url------" + response.request().url() + "\n"
+ "响应体------" + responseBody.string() + "\n"
+ "响应头------" + response.headers()
);
return response;
}
}
HttpHeaderInterceptor.java:添加请求头
public class HttpHeaderInterceptor implements Interceptor {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("Cookie","xxx");
return chain.proceed(builder.build());
}
}
|