android - How to detect a redirection in Glide v4 / OkHttp3? -
using glide v4 , okhttp3, how can detect redirection , load url when happens?
my usecase: use glide v4 library okhttp3 download pictures in app. when picture not available, redirection performed server provide picture instead of 1 originaly wanted. can see in browser because when request url a, land on url b second picture. when happens want instead load url c derived url (so not static url).
at moment can detect redirection using okhttp3 interceptor:
public response intercept(@nonnull chain chain) throws ioexception { string requesturl = chain.request().url().tostring(); response response = chain.proceed(chain.request()); string responseurl = response.request().url().tostring(); boolean redirect = !requesturl.equals(responseurl); if (redirect) { timber.d("detected redirection"); } return response; } but don't know how cleanly load url c. don't see how can load url in interceptor, , if throw ioexception handle error later in glide requestlistener result in glideexception can't determine why throw.
okhttp should automatically redirecting default , loading final content. see okhttpclient.builder.followredirects.
my own testing suggests working, e.g.
$ oksocial 'https://httpbin.org/redirect-to?url=https://i.ytimg.com/vi/dqw4w9wgxcq/maxresdefault.jpg' for detecting it, assume interceptor registered via addinterceptor instead of addnetworkinterceptor. correct?
it worth ensuring redirect via 30x response , not via html refresh.
Comments
Post a Comment