Retrofit2 HttpLoggingInterceptor Logcat
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 调用,其中正文为 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);
可能是一个新手问题,但我想知道我实际上在哪里捕获 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 调用,其中正文为 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);