Diffy Diffy -4 years ago 156
Android Question

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.

Answer Source

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();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download