Custom Downloader using Picasso

≡放荡痞女 提交于 2019-12-04 06:23:54

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!