您的位置:首页 > 移动开发

解决服务器返回错误数据格式导致Json解析出错造成app崩溃

2017-07-27 17:44 986 查看
第一种方案:可以使用try来手动抛出异常,并打印错误信息

MatchBean matchBean = null;
try {
matchBean = new Gson().fromJson(msg.obj.toString(), MatchBean.class);
} catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
LogUtils.e(str);
}


第二种方案:判断返回的数据是否为json格式

/**
* 判断是否是json结构
*/
public static boolean isJson(String value) {
try {
new JSONObject(value);
} catch (JSONException e) {
return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐