Android的毕加索库,如何添加认证头?毕加索、Android

2023-09-06 08:12:18 作者:逆天

我已经尝试设置自定义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