最近开始对APP上的Crash进行对应,发现有好多常见的问题,同一个问题在多个APP都类似的出现了,这里记录下这些常见的错误。

crash Log:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 425727 path $.shops[631].lineup; nested exception is com.google.gson.ad: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 425727 path $.shops[631].lineup
at 包名.k.readInternal(SourceFile:75)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(SourceFile:147)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(SourceFile:76)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(SourceFile:655)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(SourceFile:641)
at org.springframework.web.client.RestTemplate.doExecute(SourceFile:484)
at org.springframework.web.client.RestTemplate.execute(SourceFile:439)
at org.springframework.web.client.RestTemplate.exchange(SourceFile:415)

根据错误log的意思,应该是服务器(php开发)返回了非正常的json格式错误信息导致app崩溃。

项目背景:

项目是使用AA框架开发的,Api请求使用的是SpringRestTemplate,使用Gson进行json与Bean的转换

为了解决Gson在Android6.0上的bug,自定义了一个GsonConverter,继承自GsonHttpMessageConverter。在数据转换时添加了log,主要代码如下:

 @Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
String str = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())));
LogUtil.d("in =" + str);
try {
Type typeOfT = getType(); if (typeOfT != null) {
return this.gson.fromJson(str, typeOfT);
} else {
return this.gson.fromJson(str, clazz);
}
} catch (JsonSyntaxException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
} catch (JsonIOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
} catch (Exception ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
}

然后就是AA中Rest的配置了,将自定义的GsonConverter配置到Rest上。

在每个请求中都设置了RestErrorHandler,单纯的log出数据,并没有业务逻辑

mClient.setRestErrorHandler(handler);
@Override
public void onRestClientExceptionThrown(RestClientException e) {
LogUtil.e(e);
}

根据CrashLog,定位到问题是Api返回的数据转换成Bean出错导致的,代码定位到了GsonConverter.readInternal方法,通常来说方法上已经声明了错误类型了,按照业务逻辑抛出指定的错误类型不应该导致App崩溃,应该是回调RestErrorHandler的方法才对的。但是根据实际测试下来和猜想的还是有很大的区别。

然后抽取一个Api,代码如下:


ResponseEntity<CheckVersionResponse> entity = apiHelper.checkVersion(); if (null == entity || !entity.hasBody()) {
return;
}

如果在GsonConverter.readInternal中抛出异常,则App崩溃。如果在以上代码中添加TryCatch,则可以捕获到异常。这个就好奇了,怎么是直接抛出异常,而不会回调异常处理接口。如果是这么修改的话,整个系统几十个接口都需要修改,工程量太大而且太傻。

解决办法:

既然抛出异常会导致崩溃,那么当Api转换错误时,数据返回null不就可以了。修改后的代码:

 @Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
String str = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders())));
LogUtil.d("in =" + str);
try {
Type typeOfT = getType(); if (typeOfT != null) {
return this.gson.fromJson(str, typeOfT);
} else {
return this.gson.fromJson(str, clazz);
}
} catch (JsonSyntaxException ex) {
LogUtil.e("Could not read JSON: " + ex.getMessage(), ex);
// throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
} catch (JsonIOException ex) {
LogUtil.e("Could not read JSON: " + ex.getMessage(), ex);
// throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
} catch (Exception ex) {
LogUtil.e("Could not read JSON: " + ex.getMessage(), ex);
// throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
return null;
}

原因分析:

解决办法找到了,深究下定位个原因,AA框架自动生成的ApiClient源码:


@Override
public ResponseEntity<CheckVersionResponse> checkVersion(Map<String, Object> params) {
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(params);
try {
return restTemplate.exchange(rootUrl.concat("/checkVersion/"), HttpMethod.POST, requestEntity, CheckVersionResponse.class);
} catch (RestClientException e) {
if (restErrorHandler!= null) {
restErrorHandler.onRestClientExceptionThrown(e);
return null;
} else {
throw e;
}
}
}

从这里可以看出,只有RestClientException类型才会回调异常回调接口,其他的错误只会直接抛出。

然而HttpMessageNotReadableException不是RestClientException类型的,所以异常就直接抛出,没有被捕获当然就导致APP崩溃了。

Crash-fix-2:org.springframework.http.converter.HttpMessageNotReadableException的更多相关文章

  1. Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public xxxxxxxx.

    最近在使用 springBoot开发的时候, 使用PostMan访问接口,  返回一个 404 ,  后台报一个 warn : Failed to read HTTP message: org.spr ...

  2. org.springframework.http.converter.HttpMessageNotReadableException

    发起请求报错:org.springframework.http.converter.HttpMessageNotReadableException 查看请求头: application/json 所以 ...

  3. DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

    客户端当发送空的json字符串时,请求RestController时,报错: DefaultHandlerExceptionResolver : Failed to read HTTP message ...

  4. 异常:org.springframework.http.converter.HttpMessageNotReadableException

    spring(springboot.springmvc)出现标题的异常一般是由于controller的入参失败引起的. 介绍下常规入参的两种格式: ,这种方式的入参主要是接受key-value的参数, ...

  5. Spring 4.x org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ClassNotFoundException:

    Spring 4.x The first major version of Jackson is no longer supported in Spring 4. The class you want ...

  6. SSM框架搭建java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    在搭建 spring springMVC Mybatis 时候出错 将org.springframework.http.converter.json.MappingJacksonHttpMessage ...

  7. Not found org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    原因spring3跟spring4的jackson不一样. 解决方案: 1)spring3.x是org.springframework.http.converter.json.MappingJacks ...

  8. Springmvc 中org.springframework.http.converter.json.MappingJackson2HttpMessageConverter依赖jackson包

    1,问题详情:Spring使用4.3.5.Release版本后 在SpringMvc配置文件中配置json 解析器后出现报错信息 [org.springframework.web.context.Co ...

  9. java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    原因是Spring 3.x 和4.X处理JSON的一个类不一样,而这个东西又配置在xml文件中,所以编译时又无法发现 spring3.x是org.springframework.http.conver ...

随机推荐

  1. Certified Scrum Master CSM 中文资料大全

    课程概览 本课程由中国唯一一位获CST认证培训师及LeSS-Friendly Scrum Trainer双重认证讲师,丰富一线实战经验的Scrum教练讲授:姜信宝 BoB Jiang. 敏捷变革中心是 ...

  2. <string>头文件常用成员函数

    之前说过 string和vector一样,也是一种顺序容器,并且它也自带成员函数,用法和vector的成员函数差不多,不过它只能用来存放字符,也就是字符串. 在c++中,<string>基 ...

  3. c++使用cin、cout与c中使用scanf、printf进行输入输出的效率问题

    在c++中,我们使用cin和cout进行输入输出会比用scanf和printf更加简洁和方便,但是当程序有大量IO的时候,使用cin和cout进行输入输出会比用scanf和printf更加耗时, 在数 ...

  4. thinkphp--create()方法

    1.create方法可以对POST提交的数据进行处理(通过表中的字段名称与表单提交的名称对应关系自动封装数据实例),例如user表中有一个字段名叫"username",如果表单中有 ...

  5. linux下的.ssh文件夹路径等

    1.linux下的.ssh文件夹在~下,直接cd ~/.ssh即可 2.tp经过gd类处理过的水印图片格式为png 3.前端扒下别人家的网站如果自己本地打开有出现相同的代码段则有可能是js动态添加的, ...

  6. Adobe Flash player 过期

    完美解决问题的办法,在百度中输入 "adobe flash player debugger",如图进入官网 选择对应操作系统的对应版本,下载安装,重启浏览器,一切ok IE内核浏览 ...

  7. 2019-2020-1 20199308《Linux内核原理与分析》第四周作业

    <Linux内核分析> 第三章 MenuOS的构造 3.1 Linux内核源代码简介 操作系统的"两把宝剑" 中断上下文:保存现场和恢复现场 进程上下文 目录结构 ar ...

  8. 设计数据库 ER 图太麻烦?不妨试试这两款工具,自动生成数据库 ER 图!!!

    忙,真忙 点赞再看,养成习惯,微信搜索『程序通事』,关注就完事了! 点击查看更多精彩的文章 这两个星期真是巨忙,年前有个项目因为各种莫名原因,一直拖到这个月才开始真正测试.然后上周又接到新需求,马不停 ...

  9. 在java中使用JMH(Java Microbenchmark Harness)做性能测试

    文章目录 使用JMH做性能测试 BenchmarkMode Fork和Warmup State和Scope 在java中使用JMH(Java Microbenchmark Harness)做性能测试 ...

  10. 在okhttp的callback回调中加Toast出现Cant create handler inside hread that has not called Looper.prepare()...

    2019独角兽企业重金招聘Python工程师标准>>> 分析:callback中回调的response方法中还是在子线程中运行的,所以要调取Toast必须回到主线程中更新ui 解决方 ...