我尝试使用自定义身份验证器设置自定义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'
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在使用picasso库来下载位图,因此在api中我需要在标头中传递令牌.我尝试从这个线程Android Picasso库下面的代码,如何添加身份验证标头?
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;
}
Run Code Online (Sandbox Code Playgroud)
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, …Run Code Online (Sandbox Code Playgroud)