HttpLoggingInterceptor不显示任何日志

时间:2017-06-08 12:17:02

标签: android retrofit2

尝试将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();
    }
}

1 个答案:

答案 0 :(得分:1)

您的示例使用的是您可能尚未配置的默认记录器。更好的解决方案是使用您选择的日志框架传递自定义记录器。 Example using SLF4J with Logback

func cgPointForGridPoint(x: Int, y: Int) -> CGPoint    
{
 // Note that we are also adding half of the width and half of the 
 // height since the anchor point of the grid point is at its center

    let xOfPoint = CGFloat(x) * sizeOfEachGridPoint.width
                   + sizeOfEachGridPoint.width / 2 

    let yOfPoint = CGFloat(y) * sizeOfEachGridPoint.height
                   + sizeOfEachGridPoint.height / 2  

    return CGPoint(x: xOfPoint, y: yOfPoint)
}