[关闭]
@liter 2015-10-04T16:01:21.000000Z 字数 2763 阅读 3715

Android网络通信框架LiteHttp 第七节:重试和重定向

litehttp2.x版本系列教程


官网: http://litesuits.com
QQ群: 大群 47357508,二群 42960650

本系列文章面向android开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。

本系列文章目录总览: https://zybuluo.com/liter/note/186513


第七节:LiteHttp之重试和重定向

1. 重试和重定向

先准备一个可以重定向的地址:

  1. public String redirectUrl = "http://www.baidu.com/link?url=Lqc3GptP8u05JCRDsk0jqsAvIZh9WdtO_RkXYMYRQEm";

设置最大跳转和重试次数即可,然后开跳:

  1. // Retry/Redirect
  2. // make request
  3. StringRequest redirect = new StringRequest(redirectUrl)
  4. .setMaxRetryTimes(1) // maximum retry times
  5. .setMaxRedirectTimes(5) // maximum redirect times
  6. .setHttpListener(new HttpListener<String>() {
  7. @Override
  8. public void onRedirect(AbstractRequest<String> request, int max, int times) {
  9. Toast.makeText(activity, "Redirect max num: " + max + " , times: " + times
  10. + "\n GO-TO: " + request.getUri(), Toast.LENGTH_LONG).show();
  11. }
  12. @Override
  13. public void onRetry(AbstractRequest<String> request, int max, int times) {
  14. Toast.makeText(activity, "Retry Now! max num: " + max + " , times: " + times
  15. , Toast.LENGTH_LONG).show();
  16. }
  17. @Override
  18. public void onSuccess(String s, Response<String> response) {
  19. HttpUtil.showTips(activity, "LiteHttp2.0", "Content Length: " + s.length());
  20. }
  21. });
  22. liteHttp.executeAsync(redirect);

也可以设置全局重试和重定向次数:

  1. // default retry times
  2. liteHttp.getConfig().setDefaultMaxRetryTimes(2);
  3. // default redirect times
  4. liteHttp.getConfig().setDefaultMaxRedirectTimes(4);

如果请求本身设置了最大重试和重定向次数,以请求本身设置为准,如果未设置,则取全局默认值。

2. 关于重试的更多知识

重试出现的原因比较复杂或隐晦,网络状态不良时可能会触发,测试中难以必现。

当出现以下异常时,立即结束不作重试:

UnknownHostException
FileNotFoundException
SSLException
ConnectException

当出现以下异常时,重试发生可能性较大:

NoHttpResponseException
SocketException
SocketTimeoutException
ConnectTimeoutException

当未传入context时,线程睡眠指定时间,睡眠时间可以在config中设置,默认时间3000毫秒:

// default retry waitting time
liteHttp.getConfig().setForRetry(1500, false);

传入context时,lite-http便可以自己判断网络状态,如果网络是连接中的线程不作睡眠立即重试,当系统网络处于连接中、扫描等状态时,睡眠等待指定时间后重试。

3. 关于重定向的原理和过程

当请求状态码为 30x 时,应该处理重定向的情况。
获取 header 中 location 字段的值,即为重定向新地址。
一般情况下新地址可能不包含 scheme 和 host 信息,因此需要我们自己拼接。
当重试大于最大次数,给予抛出异常处理。

框架主要代码如下:

  1. if (response.getRedirectTimes() < maxRedirectTimes) {
  2. // get the location header to find out where to redirect to
  3. Header locationHeader = ares.getFirstHeader(Consts.REDIRECT_LOCATION);
  4. if (locationHeader != null) {
  5. String location = locationHeader.getValue();
  6. if (location != null && location.length() > 0) {
  7. if (!location.toLowerCase().startsWith("http")) {
  8. URI uri = new URI(request.getFullUri());
  9. URI redirect = new URI(uri.getScheme(), uri.getHost(), location, null);
  10. location = redirect.toString();
  11. }
  12. response.setRedirectTimes(response.getRedirectTimes() + 1);
  13. request.setUri(location);
  14. if (HttpLog.isPrint) {
  15. HttpLog.i(TAG, "Redirect to : " + location);
  16. }
  17. if (listener != null) {
  18. listener.notifyCallRedirect(request, maxRedirectTimes, response.getRedirectTimes());
  19. }
  20. connectWithRetries(request, response);
  21. return;
  22. }
  23. }
  24. throw new HttpServerException(httpStatus);
  25. } else {
  26. throw new HttpServerException(ServerException.RedirectTooMuch);
  27. }

更多知识,多看源码,抽空造个小轮子,来得更直接。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注