squeeish squeeish - 3 months ago 24
Android Question

Get redirected URL from Picasso

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.

Answer

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;
  }
});
Comments