square / picasso

A powerful image downloading and caching library for Android
https://square.github.io/picasso/
Apache License 2.0
18.72k stars 3.97k forks source link

Picasso, OkHttp: Cannot add LoggingInterceptor #937

Closed dotgc closed 9 years ago

dotgc commented 9 years ago

I'm using Picasso for my application. I need to add a LoggingInterceptor to the requests that Picasso makes. I'm using Picasso 2.5.0. along with OkHttp 2.2.0

I did the following:

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());
    }
    return picassoOkHttpClient;
}
}

public static class LoggingInterceptor implements Interceptor {
    public static final String TAG = "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;
    }
}

I don't see any requests made to my image urls. Please tell me what am I doing wrong?

JakeWharton commented 9 years ago

Are you using CustomPicasso.getInstance() to load the images? Is the app a debug build?

dotgc commented 9 years ago

Yes I'm using CustomPicasso.getInstance() to load images. It's a debug build. Nevertheless, I had also checked with the Build.DEBUG check not there

JakeWharton commented 9 years ago

Did you figure this out? It isn't a Picasso bug so if not please ask on StackOverflow as you'll get much more visibility and potentially help other future users who encounter the problem.

dotgc commented 9 years ago

Yep, this seems to be working fine in the current Picasso release.