話不多少直接上代碼陵叽。相關(guān)配置就不在贅述了扣墩。加上這個配置
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
第一步 初始化HttpLoggingInterceptor
@Provides
@Singleton
public HttpLoggingInterceptor providerHttpInter(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.d("retorfit","retorfit"+message);
}
});
//四個等級
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return httpLoggingInterceptor;
}
此處日志打印有四個等級:NONE、BASIC、HEADERS务豺、BODY匹颤。 就不在一一打印出來了。BODY就是最詳細了。
第二步 為Http設(shè)置
@Provides
@Singleton
public OkHttpClient provideOk(HttpLoggingInterceptor httpLoggingInterceptor){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(30 * 1000, TimeUnit.MILLISECONDS)
.readTimeout(20 * 1000, TimeUnit.MILLISECONDS)
.build();
return okHttpClient;
}
完工看下效果
image.png