我尝试使用自定义身份验证器设置自定义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'
谢谢!
答案 0 :(得分:84)
由于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();
答案 1 :(得分:8)
有关更新的解决方案,请参阅bryant1410's answer。
这样的事情是设置API密钥头的工作:
public class CustomPicasso {
private static Picasso sPicasso;
private CustomPicasso() {
}
public static Picasso getImageLoader(final Context context) {
if (sPicasso == null) {
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new CustomOkHttpDownloader());
sPicasso = builder.build();
}
return sPicasso;
}
private static class CustomOkHttpDownloader extends OkHttpDownloader {
@Override
protected HttpURLConnection openConnection(final Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty(Constants.HEADER_X_API_KEY, "MY_API_KEY");
return connection;
}
}
}
答案 2 :(得分:3)
您还可以按照documentation of OkHttp
中的建议添加身份验证只需添加此客户端
即可final OkHttpClient client = new OkHttpClient.Builder()
.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = okhttp3.Credentials.basic("user", "pw");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
毕加索就像这样:
final Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttp3Downloader(client))
.build();
Picasso.setSingletonInstance(picasso);
唯一需要的依赖是:
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'
答案 3 :(得分:2)
它正在运作
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.authenticator(new Authenticator()
{
@Override
public Request authenticate(Route route, Response response) throws IOException
{
String credential = Credentials.basic("username","password");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
}).build();
Picasso picasso = new Picasso.Builder(OnDemandImageCaptureActivity.this)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
picasso.load("http://example.com/abc.jpeg").into(ivcamera);
依赖性:
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
答案 4 :(得分:0)
这样的简单方法将保持默认的OkHttpClient超时和缓存配置:
private class MyOkHttpDownloader extends OkHttpDownloader {
public MyOkHttpDownloader(final Context context) {
super(context);
getClient().interceptors().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);
}
});
}
}
Picasso picasso = new Picasso.Builder(context).downloader(new MyOkHttpDownloader(context)).build();