Get redirected URL from Picasso

我怕爱的太早我们不能终老 提交于 2020-01-24 16:27:10

问题


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.


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/29952860/get-redirected-url-from-picasso

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!