android-Retrofit2 HttpLoggingInterceptor Logcat
作者:互联网
可能是一个新手问题,但我想知道我实际上在哪里捕获了log cat中的日志.有没有放置日志的地方,要使用的特殊正则表达式等?这就是我的拦截器的样子:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
// Retrofit setup
Retrofit client = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okClient)
.build();
这是一个API调用,其中body为null
retrofit2.Call<GeneralTokenResponse> generalTokenResponseCall = ApiInterface.getGeneralAccessToken(ApiGeneral.API_VERSION);
generalTokenResponseCall.enqueue(new retrofit2.Callback<GeneralTokenResponse>() {
@Override
public void onResponse(retrofit2.Call<GeneralTokenResponse> call, retrofit2.Response<GeneralTokenResponse> response) {
Log.d("DEBUG", "body: "+response.body());
}
@Override
public void onFailure(retrofit2.Call<GeneralTokenResponse> call, Throwable t) {
}
});
解决方法:
我这样使用:
// init okhttp 3 logger
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override public void log(String message) {
Log.d("MyTAG", "OkHttp: " + message);
}
});
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
标签:okhttp3,retrofit2,android-logcat,android 来源: https://codeday.me/bug/20191027/1944016.html