I am using the picasso
library to download the bitmap so in the api I need to pass the token in the headers. I tried below code from this thread Android Picasso library, How to add authentication headers?
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 Request
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);
Question
I debugged my code & I see it never called openconnection
override method of OkHttpDownloader
so my request always fail & at the end it calls onBitmapFailed
.
Please help what are things I have to do to pass headers value correctly.
Thanks in advance.
I used another library AQuery and was able to get not only authorized access to picassa rolling in a few minutes but also the library used the phones credentials so it was extremely easy.
Even if you don't use this library take a look at how I get the experimental method of including only the fields needed working below. The smaller results makes for faster network io and a huge difference in CPU. Because the JSON is smaller it parses faster and or the DOM for the xml is smaller it is built extremely fast.
Here I'm using the experimental method of returning only fields I want for public albums for the user in XML.
Picasso 2.5, The okHttpDownloader has changed. Please refer the below link to add the authentication headers
https://github.com/square/picasso/issues/900
I had the same problem but in my case I had forgotten I had an self-signed certificate on my server so OkHttp was getting the certificate and then refusing to retrieve any images. Consequently from the server side it looked like Picasso was not making any requests.
So the fix was to create an unsafe OkHttp client that doesn't test certificates:
Then use it in my CustomOkHttpDownloader:
It took two days to resolve this problem. For custom downloader you don't have to call
with
method because this will initialize the default downloader & picasso instance. Simply do below like this that will help you to get bitmap.implement Loader to BasicAuthOkHttpLoader class.
In override Load method, write ur authentication logic.
For more details: http Basic auth the implementation of a custom loader
This finally worked for me, just call it and then use the picasso instance, here I add an access token. But you could also add username and password.