问题
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:
Interceptor
s 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