我在SO上尝试了几种解决方案,但没有一种方法适合我。
如何在Android Picasso 库版本2.5.2中添加自定义 HTTP标头?
答案 0 :(得分:7)
基于Android Picasso library, How to add authentication headers?
我已经用这种方式解决了
依赖关系:
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();
感谢您的帮助