Android 截击处理重定向 [英] Android volley to handle redirect
问题描述
我最近开始使用 Google 的 Volley 库来处理我的网络请求.我的一个请求收到错误 301 进行重定向,所以我的问题是可以自动以某种方式自动处理重定向还是我必须在 parseNetworkError
中手动处理它或使用某种 RetryPolicy
>在这里?
I recently started to use Volley lib from Google for my network requests. One of my requests get error 301 for redirect, so my question is that can volley handle redirect somehow automatically or do I have to handle it manually in parseNetworkError
or use some kind of RetryPolicy
here?
谢谢.
推荐答案
我修复了它捕获 http 状态 301 或 302、读取重定向 url 并将其设置为请求然后抛出触发重试的期望.
I fixed it catching the http status 301 or 302, reading redirect url and setting it to request then throwing expection which triggers retry.
以下是我修改的 volley lib 中的主要键:
Here are the main keys in volley lib which i modified:
为
Request
在 BasicNetwork 类中添加了重定向检查//处理缓存验证后,if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) ||statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
,在那里我用 responseHeaders.get("location")
读取重定向 url,用请求对象调用 setUrl
并抛出错误
In class BasicNetwork is added check for redirection after // Handle cache validation, if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
, there I read the redirect url with responseHeaders.get("location")
, call setUrl
with request object and throw error
捕获错误并调用 attemptRetryOnException
您还需要为 Request
设置 RetryPolicy
(请参阅 DefaultRetryPolicy
)
You also need to have RetryPolicy
set for the Request
(see DefaultRetryPolicy
for this)
这篇关于Android 截击处理重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!