Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Downloader using Picasso

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.

like image 287
Diffy Avatar asked Jul 27 '14 13:07

Diffy


1 Answers

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();
}
like image 131
Vinny K Avatar answered Sep 17 '22 05:09

Vinny K