【问题标题】:Picasso, OkHttp: Cannot add LoggingInterceptorPicasso,OkHttp:无法添加 LoggingInterceptor
【发布时间】:2015-05-12 09:47:36
【问题描述】:

我将毕加索用于我的应用程序。我需要在 Picasso 提出的请求中添加一个 LoggingInterceptor。我正在使用毕加索 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;
    }
}

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

【问题讨论】:

    标签: android picasso okhttp


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-12
      • 2015-05-23
      • 2017-01-16
      • 2018-03-24
      • 2015-06-18
      • 2016-01-28
      • 2014-09-23
      • 2016-08-15
      相关资源
      最近更新 更多