我在Stack Overflow上尝试了几个解决方案,但都没有对我起作用。
如何在Android Picasso库版本2.5.2中添加自定义HTTP头?
基于Android Picasso库,如何添加身份验证标头?
我已经以这种方式解决了
依赖项:
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.5.0'
代码
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("X-CUSTOM-HEADER", "my-header-value")
.build();
return chain.proceed(newRequest);
}
});
return new Picasso.Builder(context).downloader(new OkHttpDownloader(okHttpClient)).build();
感谢您的帮助
OkHttpClient
来添加头部,然后将客户端包装在OkHttpDownloader
中,并使用它来构建Picasso
实例,然后在调用Picasso.with()
之前使用Picasso.setSingletonInstance()
设置该实例。对吗? - david.miholaPicasso.with()
?如果你能粘贴一些代码,这会更容易一些... - david.mihola