AndroidAsyncHttp 临时修复 JsonHttpResponseHandler 避免死循环
由于 AndroidAsyncHttp 1.4.4 的 JsonHttpResponseHandler 存在死循环的 BUG,1.4.5 版本发布不知道要何时,所以只能临时替换该类来修复这个错误。
Android开源库loopj的android-async-http的 JsonHttpResponseHandler 存在死循环GC_CONCURRENT
- package com.ai9475.extend;
- import com.ai9475.meitian.AppManager;
- import com.ai9475.meitian.R;
- import com.loopj.android.http.JsonHttpResponseHandler;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.util.Log;
- import org.apache.http.Header;
- import org.apache.http.HttpStatus;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- import java.io.UnsupportedEncodingException;
- /**
- * 复写 AndroidAsyncHttp 1.4.4 开源库的 JsonHttpResponseHandler 类
- * 当 1.4.5 released 后失效
- *
- * Created by ZHOUZ on 2014/3/22.
- */
- public class ZJsonHttpResponseHandler extends JsonHttpResponseHandler
- {
- private static final String LOG_TAG = "JsonHttpResponseHandler";
- /**
- * Returns when request succeeds
- *
- * @param statusCode http response status line
- * @param headers response headers if any
- * @param response parsed response if any
- */
- public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
- }
- /**
- * Returns when request succeeds
- *
- * @param statusCode http response status line
- * @param headers response headers if any
- * @param response parsed response if any
- */
- public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
- }
- /**
- * Returns when request failed
- *
- * @param statusCode http response status line
- * @param headers response headers if any
- * @param throwable throwable describing the way request failed
- * @param errorResponse parsed response if any
- */
- public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
- }
- /**
- * Returns when request failed
- *
- * @param statusCode http response status line
- * @param headers response headers if any
- * @param throwable throwable describing the way request failed
- * @param errorResponse parsed response if any
- */
- public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
- }
- @Override
- public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
- final AlertDialog.Builder dialog = new AlertDialog.Builder(AppManager.ActivityManager.current());
- dialog.setIcon(android.R.drawable.ic_dialog_info);
- dialog.setTitle(R.string.app_error);
- dialog.setMessage(responseString);
- dialog.setNegativeButton(R.string.sure,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- @Override
- public void onSuccess(int statusCode, Header[] headers, String responseString) {
- }
- @Override
- public final void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBytes) {
- if (statusCode != HttpStatus.SC_NO_CONTENT) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- final Object jsonResponse = parseResponse(responseBytes);
- postRunnable(new Runnable() {
- @Override
- public void run() {
- if (jsonResponse instanceof JSONObject) {
- onSuccess(statusCode, headers, (JSONObject) jsonResponse);
- } else if (jsonResponse instanceof JSONArray) {
- onSuccess(statusCode, headers, (JSONArray) jsonResponse);
- } else if (jsonResponse instanceof String) {
- onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
- } else {
- onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
- }
- }
- });
- } catch (final JSONException ex) {
- postRunnable(new Runnable() {
- @Override
- public void run() {
- onFailure(statusCode, headers, ex, (JSONObject) null);
- }
- });
- }
- }
- }).start();
- } else {
- onSuccess(statusCode, headers, new JSONObject());
- }
- }
- @Override
- public final void onFailure(final int statusCode, final Header[] headers, final byte[] responseBytes, final Throwable throwable) {
- if (responseBytes != null) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- final Object jsonResponse = parseResponse(responseBytes);
- postRunnable(new Runnable() {
- @Override
- public void run() {
- if (jsonResponse instanceof JSONObject) {
- onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
- } else if (jsonResponse instanceof JSONArray) {
- onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
- } else if (jsonResponse instanceof String) {
- onFailure(statusCode, headers, (String) jsonResponse, throwable);
- } else {
- onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
- }
- }
- });
- } catch (final JSONException ex) {
- postRunnable(new Runnable() {
- @Override
- public void run() {
- onFailure(statusCode, headers, ex, (JSONObject) null);
- }
- });
- }
- }
- }).start();
- } else {
- Log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
- onFailure(statusCode, headers, throwable, (JSONObject) null);
- }
- }
- /**
- * Returns Object of type {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long,
- * Double or {@link JSONObject#NULL}, see {@link org.json.JSONTokener#nextValue()}
- *
- * @param responseBody response bytes to be assembled in String and parsed as JSON
- * @return Object parsedResponse
- * @throws org.json.JSONException exception if thrown while parsing JSON
- */
- protected Object parseResponse(byte[] responseBody) throws JSONException {
- if (null == responseBody)
- return null;
- Object result = null;
- //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
- String jsonString = getResponseString(responseBody, getCharset());
- if (jsonString != null) {
- jsonString = jsonString.trim();
- if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
- result = new JSONTokener(jsonString).nextValue();
- }
- }
- if (result == null) {
- result = jsonString;
- }
- return result;
- }
- /**
- * Attempts to encode response bytes as string of set encoding
- *
- * @param charset charset to create string with
- * @param stringBytes response bytes
- * @return String of set encoding or null
- */
- public static String getResponseString(byte[] stringBytes, String charset) {
- try {
- return stringBytes == null ? null : new String(stringBytes, charset);
- } catch (UnsupportedEncodingException e) {
- Log.e(LOG_TAG, "Encoding response into string failed", e);
- return null;
- }
- }
- }
现在如果再出现 HTTP 500 或其他服务端错误(输出非 JSON 字符串)时将会直接弹出服务端页面内容的对话框方便调试了,现在只能这样了,等新版本发布后再转换下吧!
AndroidAsyncHttp 临时修复 JsonHttpResponseHandler 避免死循环的更多相关文章
- Android开源库loopj的android-async-http的 JsonHttpResponseHandler 存在死循环GC_CONCURRENT
我现在用的是 AndroidAsyncHttp 1.4.4 版本,之前遇到一个很奇怪的问题, 当使用 JsonHttpResponseHandler 解析请求的页面出现服务器错误或其他情况返回的内容不 ...
- git 入门教程之紧急修复
和往常一样,每个人团队开发者都在自己的本地分支上进行日常工作,相互独立又相互联系,一直以来相安无事,可是某天下午,上级领导突然急冲冲的打电话告诉你线上出bug了,需要你紧急修复,下班之前必须解决! 我 ...
- 使用DOS工具修复数据库
当SQL Server 实例出现异常,无法远程链接时,数据库管理员需要登陆到SQL Server实例机器上,通过命令行工具,修复异常. 一,使用net命令行启动数据库 通过net start 命令启动 ...
- 20145330 《网络对抗》 Eternalblue(MS17-010)漏洞复现与S2-045漏洞的利用及修复
20145330 <网络对抗> Eternalblue(MS17-010)漏洞利用工具实现Win 7系统入侵与S2-045漏洞的利用及修复 加分项目: PC平台逆向破解:注入shellco ...
- 关于阿里云ECS Centos 5/6/7 Linux Glibc库严重安全漏洞修复方法
日前Linux GNU glibc标准库的 gethostbyname函数爆出缓冲区溢出漏洞,漏洞编号为CVE-2015-0235.黑客可以通过gethostbyname系列函数实现远程代码执行,获取 ...
- 【转载】网络安全---Strurts2漏洞介绍
Apache Struts2 作为世界上最流行的 Java Web 服务器框架之一,3 月 7 日带来了本年度第一个高危漏洞——CVE编号 CVE-2017-5638 .其原因是由于 Apache S ...
- 参数探测(Parameter Sniffing)影响存储过程执行效率解决方案
如果SQL query中有参数,SQL Server 会创建一个参数嗅探进程以提高执行性能.该计划通常是最好的并被保存以重复利用.只是偶尔,不会选择最优的执行计划而影响执行效率. SQL Server ...
- CVE-2016-1240 Tomcat 服务本地提权漏洞
catalogue . 漏洞背景 . 影响范围 . 漏洞原理 . 漏洞PoC . 修复方案 1. 漏洞背景 Tomcat是个运行在Apache上的应用服务器,支持运行Servlet/JSP应用程序的容 ...
- plain framework 1 pak插件说明(资源压缩加密)
在互联网的发展中,资源的整理一般成了发布软件应用的迫在眉睫的一件事情,不经打包的资源往往容易暴露而且众多的文件使得拷贝等待时间变长.在这种情况下,一种应用便诞生了,其起源是源自压缩软件,这便是我们今天 ...
随机推荐
- Day4:T1小技巧(类似于指针操作)T2搜索+小细节
Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...
- 读取xml并将节点保存到Excal
using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Coll ...
- MSSQL Server查询优化方法(转)
核心提示:查询速度慢的原因很多,常见如下几种 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3. ...
- 推荐前端开发使用的服务器环境开源项目 D2Server 可替代Apache
攻欲善其事,必先利其器.前端开发,编辑器我们有了Sublime Text2,配置Server环境用……你可能会选择Apache,为什么呢?因为能供选择的选项实在太少.而现在,我向大家推荐一个针对前端开 ...
- 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 ...
- CentOS下Mysql安装教程
CentOS下Mysql安装教程 本人学习Linux时使用的是CentOs5.5版本,在该环境中,Mysql的安装方法有很多种,下面我只讲我这次成功了的方法,作为一个记录,供大家参考,同时给自己做一个 ...
- [转载]expect spawn、linux expect 用法小记
原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...
- 最近学习了下BI(商业智能)做报表
最近公司购买了Style intelligence 出的BI报表工具,接触新的东西不是很容易上手,这个东西是别的项目组用的,我们项目组由于进度比较快就让我先到他们项目组帮他们,为了使用这个东西,他们已 ...
- Objective-C非正式协议与正式协议
这两个概念困扰我很久了,一直都很像搞清楚到非正式协议和正式协议有什么区别和联系,下面结合网上的资料和自己的看法谈谈这个问题. 一.非正式协议 显然这个名词是相对于正式协议而言的.在解释非正式协议之前, ...
- Power BI官方视频(4) Power BI Desktop 2017年首次更新先睹为快
在过去的2016年,Power BI Desktop在功能上进行了很多改进和更新,产品迭代速度非常快,基本是每个月一个版本.过去的一年,我们期待的Power BI中国区服务已经可以在世纪互联购买和使用 ...