本文介绍了使用Picasso的自定义下载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从URL下载图像,该图片需要一些标题(用户名,密码)以及请求。所以我正在使用给出的代码。但调用此函数会出现错误

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)

我正在使用Picasso 2.3.3和okhttp-urlconnection-2.0.0-RC2库
该问题已在帖子,但改为2.3.2也不起作用。

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

推荐答案

你有OkHttp吗?包含在你的项目中?如果没有,问题是你正在使用OkHttpDownloader。您可以在项目中包含OkHttp库,也可以在下面添加UrlConnectionDownloader。

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

这篇关于使用Picasso的自定义下载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:27