Android HttpLoggingInterceptor的用法簡介


該攔截器用於記錄應用中的網絡請求的信息。

示例

OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
client.interceptors().add(logging);

/* 可以通過 setLevel 改變日志級別
 共包含四個級別:NONE、BASIC、HEADER、BODY

NONE 不記錄

BASIC 請求/響應行
--> POST /greeting HTTP/1.1 (3-byte body)
<-- HTTP/1.1 200 OK (22ms, 6-byte body)

HEADER 請求/響應行 + 頭

--> Host: example.com
Content-Type: plain/text
Content-Length: 3

<-- HTTP/1.1 200 OK (22ms)
Content-Type: plain/text
Content-Length: 6

BODY 請求/響應行 + 頭 + 體
*/

// 可以通過實現 Logger 接口更改日志保存位置
HttpLoggingIntercetptor logging = new HttpLoggingInterceptor(new Logger() {
    @Override
    public void log(String message) {
        Timber.tag("okhttp").d(message);
    }
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM