Android的毕加索库

Android的毕加索库

本文介绍了Android的毕加索库,如何添加认证头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试设置自定义OkHttpClient与自定义身份验证,但是作为医生说:回应对身份验证的远程Web或代理服务器挑战我必须做出2请求为每个图像,这是不理想的。

I have tried setting a custom OkHttpClient with a custom Authenticator, however as the doc says: "Responds to authentication challenges from the remote web or proxy server." I have to make 2 requests for each image, and that is not ideal.

有一个请求拦截器像改造呢?还是我失去了一些东西在OkHttpClient?

Is there a request interceptor like Retrofit does? Or am I missing something in the OkHttpClient?

我使用的是最新的版本:

I'm using the latest versions:

compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.squareup.okhttp:okhttp:2.0.+'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.+'
compile 'com.squareup.okio:okio:1.0.0'

谢谢!

推荐答案

由于毕加索2.5.0 OkHttpDownloader类已更改,所以你必须做这样的事情:

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-TOKEN", "VAL")
                    .build();
            return chain.proceed(newRequest);
        }
});

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

来源: https://github.com/square/picasso/issues/900

这篇关于Android的毕加索库,如何添加认证头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:07