Android的凌空处理重定向重定向、Android

2023-09-04 05:09:37 作者:别再遇见

我最近开始使用排LIB从谷歌为我的网络请求。我的一个请求出现错误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 RetryPolicyhere?

感谢。

推荐答案

我固定它捕获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.

编辑:这是我在其中修改凌空lib中的主键:

Here are the main keys in volley lib which i modified:

补充方法公共无效 setUrl(最后的字符串URL)要求

在类BasicNetwork添加检查重定向后//处理缓存验证,如果(状态code == HttpStatus.SC_MOVED_PERMANENTLY)||状态code == HttpStatus.SC_MOVED_TEMPORARILY),有我读了重定向URL与 responseHeaders.get(位置),叫 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

Error get's catched and it calls attemptRetryOnException

您还需要有 RetryPolicy 要求设置(见 DefaultRetryPolicy 本)