6

I have to download an image from a URL which requires some headers ( username,password) along with the request. So i am doing that using the code given here. But calling this function gives the error

java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:72)

I am using Picasso 2.3.3 and okhttp-urlconnection-2.0.0-RC2 libraries The issue has been raised in this post also but changing to 2.3.2 doesnt work.

Community
  • 1
  • 1
Diffy
  • 2,339
  • 3
  • 25
  • 47

2 Answers2

8

Do you have OkHttp included in your project? If not, the problem is that you're using the OkHttpDownloader. You can include the OkHttp library in your project or just UrlConnectionDownloader like below.

This was the result I ended up with.

public static Picasso getImageLoader(Context ctx) {
    Picasso.Builder builder = new Picasso.Builder(ctx);

    builder.downloader(new UrlConnectionDownloader(ctx) {
        @Override
        protected HttpURLConnection openConnection(Uri uri) throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            connection.setRequestProperty("X-HEADER", "VAL");
            return connection;
        }
    });

    return builder.build();
}
Vinny K
  • 194
  • 15
  • I am including okhttp-urlconnection-2.0.0-RC2 library. So do i have to include okHttp also? – Diffy Jul 28 '14 at 07:33
  • Based on [http://stackoverflow.com/a/24183951/1268021](this) answer where he says include okhttp-urlconnection _also_, I'd venture to guess you do need the standard okhttp library. The key is simply the ConnectionDownloader. You can use the UrlConnectionDownloader instead, if you'd like. – Vinny K Jul 29 '14 at 13:22
  • i am using this to call getImageLoader(context).load(uri).into(image); So will it pass the uri parameter to UrlConnectionDownloader? – Diffy Jul 30 '14 at 19:23
4

Since Picasso 2.5.0 OkHttpDownloader class has been changed, so you have to do something like this:

OkHttpClient picassoClient = new OkHttpClient();

picassoClient.networkinterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request newRequest = chain.request().newBuilder()
                    .addHeader("X-HEADER", "VAL")
                    .build();
            return chain.proceed(newRequest);
        }
});

new Picasso.Builder(context).downloader(new OkHttpDownloader(picassoClient)).build();

Source: https://github.com/square/picasso/issues/900

bryant1410
  • 5,540
  • 4
  • 39
  • 40