列印完整的okhttp網路請求和響應訊息
阿新 • • 發佈:2018-12-13
背景
開發中,有時需要核對介面請求和響應引數,需要看到詳細的介面呼叫。本來可以通過Facebook Stetho來監控介面的,但是受限於網路,導致調測介面打不開,所以只要尋求其他方案。
解決方案
通過okhttp新增攔截器,列印介面呼叫日誌。
- 新增依賴
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
- 新增攔截器
HttpLogInterceptor.java
import android.util.Log; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.concurrent.TimeUnit; import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import okhttp3.ResponseBody; import okio.Buffer; import okio.BufferedSource; /** * okhttp 攔截器 */ public class HttpLogInterceptor implements Interceptor { private static final String TAG = HttpLogInterceptor.class.getSimpleName(); private final Charset UTF8 = Charset.forName("UTF-8"); @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); RequestBody requestBody = request.body(); String body = null; if (requestBody != null) { Buffer buffer = new Buffer(); requestBody.writeTo(buffer); Charset charset = UTF8; MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } body = buffer.readString(charset); } Log.d(TAG, "傳送請求: method:" + request.method() + "\nurl:" + request.url() + "\n請求頭:" + request.headers() + "\n請求引數: " + body); long startNs = System.nanoTime(); Response response = chain.proceed(request); long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); ResponseBody responseBody = response.body(); String rBody; BufferedSource source = responseBody.source(); source.request(Long.MAX_VALUE); Buffer buffer = source.buffer(); Charset charset = UTF8; MediaType contentType = responseBody.contentType(); if (contentType != null) { try { charset = contentType.charset(UTF8); } catch (UnsupportedCharsetException e) { e.printStackTrace(); } } rBody = buffer.clone().readString(charset); Log.d(TAG, "收到響應: code:" + response.code() + "\n請求url:" + response.request().url() + "\n請求body:" + body + "\nResponse: " + rBody); return response; } }
新增攔截器(該攔截器需要最後新增,否則其他對request、response做修改的Interceptor所導致的變更就無法打印出來的。):
Interceptor loggingInterceptor = new HttpLoggingInterceptor();
okBuilder.addInterceptor(loggingInterceptor)
-
打印出的日誌樣例:
-
如果想要格式化輸出的訊息格式,可以參考《利用logger列印完整的okhttp網路請求和響應日誌》
安卓開發技術分享: https://blog.csdn.net/yinxing2008/article/details/84555061