Get redirected URL from Picasso - android

I'm fetching my images using the following code:
Picasso.with(mContext)
.load(myImage.getUrl())
.fetch();
myImage.getUrl() returns a URL from my server, which will redirect to the actual image hosted on another server. Is there a way to catch the URL my server returns to Picasso? I know I can use a Callback in .fetch(), but that's all I know. I'm using OkHttp as well.

OkHttp allows you not to follow redirects automatically:
OkHttpClient client = new OkHttpClient();
client.setFollowRedirects(false);
You can read the response, get the redirect URL and then forward it manually to Picasso.
EDIT:
Interceptors are feasible as well:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
// process response here
return response;
}
});

I fixed it by this code.
val downloader = OkHttp3Downloader(context)
Picasso.Builder(context).downloader(downloader).build()
Check this for detail.
https://github.com/square/picasso/issues/463

Add okhttp dependency
compile 'com.squareup.okhttp:okhttp:2.5.0'
and try this code
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new OkHttpDownloader(context));
builder.build()
.load(path.trim())
.into(imageView);
This code working for me

Related

How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

I am using picasso 2.5.2 library to download bitmap from remote server, the image url requires basic authentication in header.
i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries.
Answer - 1
Answer - 2
Answer - 3
Thanks in advance.
Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.authenticator(new Authenticator()
{
#Override
public Request authenticate(Route route, Response response) throws IOException
{
String credential = Credentials.basic("user", "pass");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
Then, use that client in forming your Picasso object, but with okhttp3 you will have to use a OkHttp3Downloader instead, like so:
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(okHttpClient))
.build();
You can get the OkHttp3Downloader from https://github.com/JakeWharton/picasso2-okhttp3-downloader

Freso: How can I set the OK Http client?

So some images I request require an authentication header to be added
I am using Retrofit 2.0 which has this OkHttp client with a interceptor to add the user token to the header to every request
okHttpClient.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request(); //Current Request
Request requestWithToken = null; //The request with the access token which we will use if we have one instead of the original
requestWithToken = originalRequest.newBuilder().addHeader(Constants.UrlParamConstants.HEADER_AUTHORIZATION,String.format(Constants.UrlParamConstants.HEADER_AUTHORIZATION_VALUE, MyApplication.getInstance().getUser().getApiToken())).build();
Response response = chain.proceed((requestWithToken != null ? requestWithToken : originalRequest)); //proceed with the request and get the response
return response;
}
});
I would like to know how can I set the same okHttp client instance for Fresco library.
I am aware that you need to add this dependency to use OkHttp with Fresco but how about setting the client?
compile "com.facebook.fresco:imagepipeline-okhttp:0.8.0+"
At the end of the day I just need to set authentication header for an image request
thanks for reading
http://frescolib.org/docs/using-other-network-layers.html
Context context;
OkHttpClient okHttpClient; // build on your own
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
.newBuilder(context, okHttpClient)
. // other setters
. // setNetworkFetcher is already called for you
.build();
Fresco.initialize(context, config);

android picasso library add token header

I have been breaking my brain on this one,
I am using Picasso library to load and download images from my server, but now I want to add a header in my download request and I cant seem to find a way of doing it. all i want to do is set a header like :
setHeader("Authorization", "Bearer " + token);
I use this header in any of my server requests, but cant find a way to add it to the picasso line.
Any help would be appreciated, Thanks!
Picasso uses OkHttp as engine , or it is possible to configure Picasso to use it, and since you have to set the header of the http request, you can use an Interceptor. E.g. this is my Interceptor to handle basic authentication:
private static class BasicAuthInterceptor implements Interceptor {
#Override
public Response intercept(Chain chain) throws IOException {
final Request original = chain.request();
final Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "Basic " + BASIC_AUTH_ENCODED)
.method(original.method(), original.body());
return chain.proceed(requestBuilder.build());
}
}
and the you add the Interceptor to OkHttp like
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new BasicAuthInterceptor());
Last step is to configure Picasso to use okHttpClient.
The Picasso's builder provide a method for it :
new Picasso.Builder(context).downloader(new OkHttpDownloader(okHttpClient)).build();
gradle dependencies:
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.picasso:picasso:2.5.0'

Opening the connection using okhttp

I'm trying to Open the connection using okhttp.
something like,
urlConnection = client.open(url);
does not work with the new ok-http.jar file.
It was working with 1.5.x of okhttp version
Any suggestions?
Thanks
Code from documentation
public static void main(String[] args) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://kenumir.pl/")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
Method execute is the key ;-)
What does it means "it does not work"??? Does it fails at compile time or runtime? What kind of error does it shows? As of OkHttip 2.x.x, there's been a change in the way to open HttpUrlConnections, you need to include a new module and this should work:
// OkHttp 1.x:
HttpURLConnection connection = client.open(url);
// OkHttp 2.x:
HttpURLConnection connection = new OkUrlFactory(client).open(url);
see OkHttp Release notes for more information .

Custom Downloader using Picasso

I have to download an image from a URL which requires some headers ( username,password) along with the request. So i am doing that using the code given here. But calling this function gives the error
java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:72)
I am using Picasso 2.3.3 and okhttp-urlconnection-2.0.0-RC2 libraries
The issue has been raised in this post also but changing to 2.3.2 doesnt work.
Do you have OkHttp included in your project? If not, the problem is that you're using the OkHttpDownloader. You can include the OkHttp library in your project or just UrlConnectionDownloader like below.
This was the result I ended up with.
public static Picasso getImageLoader(Context ctx) {
Picasso.Builder builder = new Picasso.Builder(ctx);
builder.downloader(new UrlConnectionDownloader(ctx) {
#Override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
connection.setRequestProperty("X-HEADER", "VAL");
return connection;
}
});
return builder.build();
}
Since Picasso 2.5.0 OkHttpDownloader class has been changed, so you have to do something like this:
OkHttpClient picassoClient = new OkHttpClient();
picassoClient.networkinterceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("X-HEADER", "VAL")
.build();
return chain.proceed(newRequest);
}
});
new Picasso.Builder(context).downloader(new OkHttpDownloader(picassoClient)).build();
Source: https://github.com/square/picasso/issues/900

Categories

Resources