我尝试使用自定义身份验证器设置自定义 OkHttpClient,但是正如文档所说:“响应来自远程 Web 或代理服务器的身份验证挑战。”我必须对每张图片提出 2 个请求,这并不理想。
有没有像 Retrofit 这样的请求拦截器?还是我在 OkHttpClient 中遗漏了什么?
我使用的是最新版本:
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'
谢谢!
最佳答案
由于 Picasso 2.5.0 OkHttpDownloader
类已更改,假设您使用的是 OkHttp3(因此 picasso2-okhttp3-downloader ),因此您必须执行以下操作:
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("X-TOKEN", "VAL")
.build();
return chain.proceed(newRequest);
}
})
.build();
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build();
关于Android Picasso 库,如何添加身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24273783/