Interceptors
Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here's a simple interceptor that logs the outgoing request and the incoming response.
插入器是一個很有用技巧 ,可以監(jiān)控,重寫和重新請求茬贵。下面舉個栗子雄妥, 攔截器日志優(yōu)于請求和即將到來響應渤弛;
A call to chain.proceed(request)is a critical part of each interceptor’s implementation. This simple-looking method is where all the HTTP work happens, producing a response to satisfy the request.Interceptors can be chained. Suppose you have both a compressing interceptor and a check summing interceptor: you'll need to decide whether data is compressed and then checksummed, or checksummed and then compressed. OkHttp uses lists to track interceptors, and interceptors are called in order.
一個請求 chain.proceed(request) 是一個臨界的攔截器實現(xiàn)的部分己肮,它簡單可見的方法是對所有Http線程發(fā)生主穗,生產(chǎn)響應對滿意的請求圃伶,攔截器可以被束縛堤如,支持你所有壓縮攔截和求和攔截,你將需要決定是數(shù)據(jù)壓縮然后檢驗窒朋,或者檢驗和再壓縮搀罢。okhttp使用Lists集合去追蹤攔截器,攔截器順便被調(diào)用侥猩;
Interceptors are registered as either?application?or?network?interceptors. We'll use theLoggingInterceptor defined above to show the difference.
Register anapplication?interceptor by calling addInterceptor() onOkHttpClient.Builder:
攔截器注冊既不是應用也非網(wǎng)絡攔截器魄揉,我們將使用這個LoggingInterceptor定義顯示不同之處,注冊在Application攔截器 通過調(diào)用addInterceptor方法在okHttpClient.Builder;
執(zhí)行結(jié)果:?