Picasso,OkHttp:无法添加LoggingInterceptor

时间:2015-03-10 19:30:16

标签: android picasso okhttp

我使用Picasso作为我的应用程序。我需要为Picasso发出的请求添加一个LoggingInterceptor。我使用的是Picasso 2.5.0。以及OkHttp 2.2.0

我做了以下事情:

public class CustomPicasso {

public static final String TAG = "Picasso";

private static Picasso instance;

public static Picasso getInstance(Context context) {
    if (instance == null) {
        Log.d(TAG, "initializing  a new instance");
        instance = new Picasso.Builder(context.getApplicationContext())
                .downloader(new OkHttpDownloader(getClient()))
                .build();
    }
    return instance;
}

public static OkHttpClient getClient() {
    OkHttpClient picassoOkHttpClient = new OkHttpClient();
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Adding network interceptors to picasso instance");
        picassoOkHttpClient.interceptors().add(new LoggingInterceptor("PicassoLog"));
    }
    return picassoOkHttpClient;
}
}

public static class LoggingInterceptor implements Interceptor {
    public String TAG = "LoggingInterceptor";

    public LoggingInterceptor() {};

    public LoggingInterceptor(String tag) {
        TAG = tag;
        Log.d(TAG, "added logginginterceptor");
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Log.d(TAG, "Intercepting requests");

        Request request = chain.request();

        long t1 = System.nanoTime();
        Log.d(TAG, String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));
        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        Log.d(TAG, String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers()));

        return response;
    }
}

我没有看到对我的图片网址发出的任何请求。请告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果您使用okhttp 2.1.0,则可能会获得预期的结果。这是okhttp所依赖的picasso 2.5.0版本(请参阅其pom.xml文件)。