关于开源项目android-async-http
来源:互联网 发布:淘宝手机游戏交易平台 编辑:程序博客网 时间:2024/06/05 00:29
前几天CSDN上有个介绍github上比较火的android开源项目,其中一个就是android-async-http,于是就下载下来学习了下。看了下源码,其实就是对apache httpclient的封装,可能httpclient使用起来比较繁琐,经这么封装以后,使用起来就很简单了,只需要在回调函数里写上自己想实现的代码即可。有一个功能我比较感兴趣,就是发生异常时的请求retry功能。以下代码就可以实现
httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));
注意这里httpClient是DefaultHttpClient
其实httpclient里就有这个功能,这里还是进行了封装。项目中的RetryHandler类通过实现HttpRequestRetryHandler接口来实现retry功能。当异常发生时就会调用RetryHandler类中的public boolean retryRequest(IOException exception, int executionCount, HttpContext context)函数。但在这里有个异常黑名单,白名单,默认InterruptedIOException异常被放入到黑名单里去了,也就是说当这个异常发生后是不会进行retry的。InterruptedIOException有两个子类异常,ConnectTimeoutException, SocketTimeoutException,发生发生时也不会retry。但是我想要在发生这个两个异常时进行retry,于是将InterruptedIOException异常从异常黑名单中删掉。这时我以为当ConnectTimeoutException异常发生时就会触发retryRequest函数。但是当执行到
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST );
String requestType = currentReq.getMethod();
就不再执行了,也不报错,retry功能也就实现不了了。而我在用httpclient实现的时候,当ConnectTimeoutException异常发生时,String requestType = currentReq.getMethod();是报错的,报空指针异常,应该是
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute( ExecutionContext.HTTP_REQUEST );
这句返回的是空,于是在String requestType = currentReq.getMethod();之前加上个判断currentReq是否为空就解决这个问题了。
if (currentReq != null) {
String requestType = currentReq.getMethod();retry = !requestType.equals("POST");
}
- 关于开源项目android-async-http
- 开源项目之Android async-http
- 关于android-async-http
- android-async-http开源项目介绍及使用方法
- android-async-http开源项目介绍及使用方法
- android-async-http开源项目介绍及使用方法
- android-async-http开源项目介绍及使用方法
- android-async-http开源项目介绍及使用方法
- 开源项目之android-async-http开源库
- 开源项目之Android async-http(异步 HTTP 客户端开发包)
- android开发步步为营之67:使用android开源项目android-async-http异步下载文件
- 开源框架android-async-http使用
- 开源框架android-async-http使用
- 开源框架android-async-http使用
- 开源框架android-async-http使用
- android-async-http开源解析
- android-async-http开源框架学习
- Android---利用android-async-http开源项目返回json数据
- 如何向App Store提交应用
- linux中断处理浅析
- 算法复杂度速查表
- 二叉树 - 建立
- 机器学习中PCA和ICA的理解
- 关于开源项目android-async-http
- 荷兰国旗问题
- C#中字符串转换为计算公式,并进行计算的方法(自定义公式的计算)
- python实现的宏解析工具
- error: Error: No resource found that matches the given name (at 'text' with value '@string/
- Android之下拉框(Spinner)的使用
- Android_UI_点击按钮切换背景效果实现
- 比memcpy更快的内存拷贝
- Best Time to Buy and Sell Stock