由于 AndroidAsyncHttp 1.4.4 的 JsonHttpResponseHandler  存在死循环的 BUG,1.4.5 版本发布不知道要何时,所以只能临时替换该类来修复这个错误。

Android开源库loopj的android-async-http的 JsonHttpResponseHandler 存在死循环GC_CONCURRENT

  1. package com.ai9475.extend;
  2.  
  3. import com.ai9475.meitian.AppManager;
  4. import com.ai9475.meitian.R;
  5. import com.loopj.android.http.JsonHttpResponseHandler;
  6.  
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.util.Log;
  10.  
  11. import org.apache.http.Header;
  12. import org.apache.http.HttpStatus;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import org.json.JSONTokener;
  17.  
  18. import java.io.UnsupportedEncodingException;
  19.  
  20. /**
  21. * 复写 AndroidAsyncHttp 1.4.4 开源库的 JsonHttpResponseHandler 类
  22. * 当 1.4.5 released 后失效
  23. *
  24. * Created by ZHOUZ on 2014/3/22.
  25. */
  26. public class ZJsonHttpResponseHandler extends JsonHttpResponseHandler
  27. {
  28. private static final String LOG_TAG = "JsonHttpResponseHandler";
  29.  
  30. /**
  31. * Returns when request succeeds
  32. *
  33. * @param statusCode http response status line
  34. * @param headers response headers if any
  35. * @param response parsed response if any
  36. */
  37. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  38.  
  39. }
  40.  
  41. /**
  42. * Returns when request succeeds
  43. *
  44. * @param statusCode http response status line
  45. * @param headers response headers if any
  46. * @param response parsed response if any
  47. */
  48. public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
  49.  
  50. }
  51.  
  52. /**
  53. * Returns when request failed
  54. *
  55. * @param statusCode http response status line
  56. * @param headers response headers if any
  57. * @param throwable throwable describing the way request failed
  58. * @param errorResponse parsed response if any
  59. */
  60. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  61.  
  62. }
  63.  
  64. /**
  65. * Returns when request failed
  66. *
  67. * @param statusCode http response status line
  68. * @param headers response headers if any
  69. * @param throwable throwable describing the way request failed
  70. * @param errorResponse parsed response if any
  71. */
  72. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
  73.  
  74. }
  75.  
  76. @Override
  77. public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
  78. final AlertDialog.Builder dialog = new AlertDialog.Builder(AppManager.ActivityManager.current());
  79. dialog.setIcon(android.R.drawable.ic_dialog_info);
  80. dialog.setTitle(R.string.app_error);
  81. dialog.setMessage(responseString);
  82. dialog.setNegativeButton(R.string.sure,
  83. new DialogInterface.OnClickListener() {
  84. @Override
  85. public void onClick(DialogInterface dialog, int which) {
  86. dialog.dismiss();
  87. }
  88. });
  89. dialog.show();
  90. }
  91.  
  92. @Override
  93. public void onSuccess(int statusCode, Header[] headers, String responseString) {
  94.  
  95. }
  96.  
  97. @Override
  98. public final void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBytes) {
  99. if (statusCode != HttpStatus.SC_NO_CONTENT) {
  100. new Thread(new Runnable() {
  101. @Override
  102. public void run() {
  103. try {
  104. final Object jsonResponse = parseResponse(responseBytes);
  105. postRunnable(new Runnable() {
  106. @Override
  107. public void run() {
  108. if (jsonResponse instanceof JSONObject) {
  109. onSuccess(statusCode, headers, (JSONObject) jsonResponse);
  110. } else if (jsonResponse instanceof JSONArray) {
  111. onSuccess(statusCode, headers, (JSONArray) jsonResponse);
  112. } else if (jsonResponse instanceof String) {
  113. onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
  114. } else {
  115. onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
  116. }
  117.  
  118. }
  119. });
  120. } catch (final JSONException ex) {
  121. postRunnable(new Runnable() {
  122. @Override
  123. public void run() {
  124. onFailure(statusCode, headers, ex, (JSONObject) null);
  125. }
  126. });
  127. }
  128. }
  129. }).start();
  130. } else {
  131. onSuccess(statusCode, headers, new JSONObject());
  132. }
  133. }
  134.  
  135. @Override
  136. public final void onFailure(final int statusCode, final Header[] headers, final byte[] responseBytes, final Throwable throwable) {
  137. if (responseBytes != null) {
  138. new Thread(new Runnable() {
  139. @Override
  140. public void run() {
  141. try {
  142. final Object jsonResponse = parseResponse(responseBytes);
  143. postRunnable(new Runnable() {
  144. @Override
  145. public void run() {
  146. if (jsonResponse instanceof JSONObject) {
  147. onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
  148. } else if (jsonResponse instanceof JSONArray) {
  149. onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
  150. } else if (jsonResponse instanceof String) {
  151. onFailure(statusCode, headers, (String) jsonResponse, throwable);
  152. } else {
  153. onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
  154. }
  155. }
  156. });
  157.  
  158. } catch (final JSONException ex) {
  159. postRunnable(new Runnable() {
  160. @Override
  161. public void run() {
  162. onFailure(statusCode, headers, ex, (JSONObject) null);
  163. }
  164. });
  165.  
  166. }
  167. }
  168. }).start();
  169. } else {
  170. Log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
  171. onFailure(statusCode, headers, throwable, (JSONObject) null);
  172. }
  173. }
  174.  
  175. /**
  176. * Returns Object of type {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long,
  177. * Double or {@link JSONObject#NULL}, see {@link org.json.JSONTokener#nextValue()}
  178. *
  179. * @param responseBody response bytes to be assembled in String and parsed as JSON
  180. * @return Object parsedResponse
  181. * @throws org.json.JSONException exception if thrown while parsing JSON
  182. */
  183. protected Object parseResponse(byte[] responseBody) throws JSONException {
  184. if (null == responseBody)
  185. return null;
  186. Object result = null;
  187. //trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
  188. String jsonString = getResponseString(responseBody, getCharset());
  189. if (jsonString != null) {
  190. jsonString = jsonString.trim();
  191. if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
  192. result = new JSONTokener(jsonString).nextValue();
  193. }
  194. }
  195. if (result == null) {
  196. result = jsonString;
  197. }
  198. return result;
  199. }
  200.  
  201. /**
  202. * Attempts to encode response bytes as string of set encoding
  203. *
  204. * @param charset charset to create string with
  205. * @param stringBytes response bytes
  206. * @return String of set encoding or null
  207. */
  208. public static String getResponseString(byte[] stringBytes, String charset) {
  209. try {
  210. return stringBytes == null ? null : new String(stringBytes, charset);
  211. } catch (UnsupportedEncodingException e) {
  212. Log.e(LOG_TAG, "Encoding response into string failed", e);
  213. return null;
  214. }
  215. }
  216. }

现在如果再出现 HTTP 500 或其他服务端错误(输出非 JSON 字符串)时将会直接弹出服务端页面内容的对话框方便调试了,现在只能这样了,等新版本发布后再转换下吧!

AndroidAsyncHttp 临时修复 JsonHttpResponseHandler 避免死循环的更多相关文章

  1. Android开源库loopj的android-async-http的 JsonHttpResponseHandler 存在死循环GC_CONCURRENT

    我现在用的是 AndroidAsyncHttp 1.4.4 版本,之前遇到一个很奇怪的问题, 当使用 JsonHttpResponseHandler 解析请求的页面出现服务器错误或其他情况返回的内容不 ...

  2. git 入门教程之紧急修复

    和往常一样,每个人团队开发者都在自己的本地分支上进行日常工作,相互独立又相互联系,一直以来相安无事,可是某天下午,上级领导突然急冲冲的打电话告诉你线上出bug了,需要你紧急修复,下班之前必须解决! 我 ...

  3. 使用DOS工具修复数据库

    当SQL Server 实例出现异常,无法远程链接时,数据库管理员需要登陆到SQL Server实例机器上,通过命令行工具,修复异常. 一,使用net命令行启动数据库 通过net start 命令启动 ...

  4. 20145330 《网络对抗》 Eternalblue(MS17-010)漏洞复现与S2-045漏洞的利用及修复

    20145330 <网络对抗> Eternalblue(MS17-010)漏洞利用工具实现Win 7系统入侵与S2-045漏洞的利用及修复 加分项目: PC平台逆向破解:注入shellco ...

  5. 关于阿里云ECS Centos 5/6/7 Linux Glibc库严重安全漏洞修复方法

    日前Linux GNU glibc标准库的 gethostbyname函数爆出缓冲区溢出漏洞,漏洞编号为CVE-2015-0235.黑客可以通过gethostbyname系列函数实现远程代码执行,获取 ...

  6. 【转载】网络安全---Strurts2漏洞介绍

    Apache Struts2 作为世界上最流行的 Java Web 服务器框架之一,3 月 7 日带来了本年度第一个高危漏洞——CVE编号 CVE-2017-5638 .其原因是由于 Apache S ...

  7. 参数探测(Parameter Sniffing)影响存储过程执行效率解决方案

    如果SQL query中有参数,SQL Server 会创建一个参数嗅探进程以提高执行性能.该计划通常是最好的并被保存以重复利用.只是偶尔,不会选择最优的执行计划而影响执行效率. SQL Server ...

  8. CVE-2016-1240 Tomcat 服务本地提权漏洞

    catalogue . 漏洞背景 . 影响范围 . 漏洞原理 . 漏洞PoC . 修复方案 1. 漏洞背景 Tomcat是个运行在Apache上的应用服务器,支持运行Servlet/JSP应用程序的容 ...

  9. plain framework 1 pak插件说明(资源压缩加密)

    在互联网的发展中,资源的整理一般成了发布软件应用的迫在眉睫的一件事情,不经打包的资源往往容易暴露而且众多的文件使得拷贝等待时间变长.在这种情况下,一种应用便诞生了,其起源是源自压缩软件,这便是我们今天 ...

随机推荐

  1. Day4:T1小技巧(类似于指针操作)T2搜索+小细节

    Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...

  2. 读取xml并将节点保存到Excal

    using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Coll ...

  3. MSSQL Server查询优化方法(转)

    核心提示:查询速度慢的原因很多,常见如下几种 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3. ...

  4. 推荐前端开发使用的服务器环境开源项目 D2Server 可替代Apache

    攻欲善其事,必先利其器.前端开发,编辑器我们有了Sublime Text2,配置Server环境用……你可能会选择Apache,为什么呢?因为能供选择的选项实在太少.而现在,我向大家推荐一个针对前端开 ...

  5. Vim 7.4.1952 with Python/Ruby/Lua/Perl/C Syntax built for Ubuntu 16.04 x86_64

    The default Vim provided by Ubuntu 16.04 even did not have Python support. That's insane. I say, wha ...

  6. CentOS下Mysql安装教程

    CentOS下Mysql安装教程 本人学习Linux时使用的是CentOs5.5版本,在该环境中,Mysql的安装方法有很多种,下面我只讲我这次成功了的方法,作为一个记录,供大家参考,同时给自己做一个 ...

  7. [转载]expect spawn、linux expect 用法小记

    原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...

  8. 最近学习了下BI(商业智能)做报表

    最近公司购买了Style intelligence 出的BI报表工具,接触新的东西不是很容易上手,这个东西是别的项目组用的,我们项目组由于进度比较快就让我先到他们项目组帮他们,为了使用这个东西,他们已 ...

  9. Objective-C非正式协议与正式协议

    这两个概念困扰我很久了,一直都很像搞清楚到非正式协议和正式协议有什么区别和联系,下面结合网上的资料和自己的看法谈谈这个问题. 一.非正式协议 显然这个名词是相对于正式协议而言的.在解释非正式协议之前, ...

  10. Power BI官方视频(4) Power BI Desktop 2017年首次更新先睹为快

    在过去的2016年,Power BI Desktop在功能上进行了很多改进和更新,产品迭代速度非常快,基本是每个月一个版本.过去的一年,我们期待的Power BI中国区服务已经可以在世纪互联购买和使用 ...