我正在使用picasso
库来下载位图,因此在api中需要在头部传递token。我尝试了这篇帖子中的以下代码:Android Picasso library,如何添加身份验证标头?
public static Picasso getImageLoader(final Context context) {
// fetch the auth value
sSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new OkHttpDownloader(context) {
@Override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty(Constant.HEADER_X_API_KEY, sSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
return connection;
}
});
sPicasso = builder.build();
return sPicasso;
}
Picasso 请求
mTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
mdpImageView.setImageBitmap(bitmap);
Logger.d(TAG, "Test");
}
@Override
public void onBitmapFailed(Drawable drawable) {
Logger.d(TAG, "Test");
}
@Override
public void onPrepareLoad(Drawable drawable) {
Logger.d(TAG, "Test");
}
};
CustomPicasso.getImageLoader(getActivity()).with(getActivity()).load(URL).into(mTarget);
问题
我调试了我的代码,发现它从未调用OkHttpDownloader
的openconnection
覆盖方法,因此我的请求总是失败,并最终调用onBitmapFailed
。
请帮助我了解如何正确传递标头值。
提前致谢。