尝试将 HttpLoggingInterceptor 与 Retrofit2 一起使用,但没有结果,任何请求/响应都不会显示任何日志。
我使用的是图书馆的版本:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
我在其中生成 api 实例的代码:
public class ApiGenerator {
private static final String BASE_URL = "http://api.openweathermap.org/data/2.5/";
private static final String API_KEY = "de4d4a35830d98e785a1cbb06725457f";
private ApiGenerator() {}
public static WeatherApi createWeatherApi() {
return createRetrofit().create(WeatherApi.class);
}
private static Retrofit createRetrofit() {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(createClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
private static OkHttpClient createClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder().addInterceptor(chain -> {
Request request = chain.request();
HttpUrl url = request.url().newBuilder()
.addQueryParameter("APPID", API_KEY)
.build();
request.newBuilder().url(url).build();
return chain.proceed(request);
})
.addInterceptor(logging)
.build();
}
}
最佳答案
您的示例使用的是您可能未配置的默认记录器。更好的解决方案是使用您选择的日志记录框架传入自定义记录器。 Example using SLF4J with Logback .
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);
private static final HttpLoggingInterceptor loggingInterceptor =
new HttpLoggingInterceptor((msg) -> {
log.debug(msg);
});
static {
loggingInterceptor.setLevel(Level.BODY);
}
public static HttpLoggingInterceptor getLoggingInterceptor() {
return loggingInterceptor;
}
关于android - HttpLoggingInterceptor 不显示任何日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44435562/