Android的凌空处理重定向 [英] Android volley to handle redirect

查看:377
本文介绍了Android的凌空处理重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用排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 本)

这篇关于Android的凌空处理重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆