解决Java字符串JSON转换异常:cn.hutool.json.JSONException: Mismatched hr and body
报错起因:
1、在调用外部api接口,获取JSON字符串,通过各种JSON文件转对象的时候发现报错。
String post = HttpUtil.post(url, JSONUtil.toJsonStr(params)); JSONObject jsonObject = JSONUtil.parseObj(post);
报错信息如下:
cn.hutool.json.JSONException: Mismatched hr and body at 164 [character 6 line 6]
at cn.hutool.json.JSONTokener.syntaxError(JSONTokener.java:413)
at cn.hutool.json.xml.JSONXMLParser.parse(JSONXMLParser.java:99)
at cn.hutool.json.xml.JSONXMLParser.parse(JSONXMLParser.java:164)
at cn.hutool.json.xml.JSONXMLParser.parse(JSONXMLParser.java:164)
at cn.hutool.json.xml.JSONXMLParser.parse(JSONXMLParser.java:164)
at cn.hutool.json.xml.JSONXMLParser.parseJSONObject(JSONXMLParser.java:29)
at cn.hutool.json.XML.toJSONObject(XML.java:101)
at cn.hutool.json.ObjectMapper.mapFromStr(ObjectMapper.java:216)
at cn.hutool.json.ObjectMapper.map(ObjectMapper.java:98)
at cn.hutool.json.JSONObject.<init>(JSONObject.java:210)
at cn.hutool.json.JSONObject.<init>(JSONObject.java:187)
at cn.hutool.json.JSONObject.<init>(JSONObject.java:142)
at cn.hutool.json.JSONObject.<init>(JSONObject.java:125)
at cn.hutool.json.JSONUtil.parseObj(JSONUtil.java:88)
排查过程:
一开始,我一直以为是因为hutool 工具,或者其他JSON有转换的异常,百度,谷歌很多方案,只提示了问题是xml转body错误。
一般这种问题还是要看你转换报错信息,最好给日志打印出来,后来通过try,catch,以及日志打印异常,最终知道报错原因:
<html> <head><title>504 Gateway Time-out</title></head> <body bgcolor="white"> <center><h1>504 Gateway Time-out</h1></center> <hr><center>nginx</center> </body> </html> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page -->
总结: 问题出现在http请求外部接口的时候,有时候,可能存在网络抖动,后者过于频繁的请求接口,导致接口偶发性的出现504网络异常。导致自己需要查找的数据没有查找到,或者解析出来。
最终解决办法:
通过重试机制,三次延时调用接口,提高对调用接口这种偶发性的问题的解决方案。
JSONObject jsonObject = null; int count = 0; while (count < 3) { try { String post = HttpUtil.post("url", JSONUtil.toJsonStr(params)); jsonObject = JSONUtil.parseObj(post); count = 3; } catch (Exception e) { log.error("JSON异常:{}", e.getMessage()); count++; try { log.warn("=============第{}次等待{}秒后重新请求接口=============", count, 5 * count); Thread.sleep(5000 * count); } catch (InterruptedException interruptedException) { interruptedException.printStackTrace(); } } }
总结
到此这篇关于解决Java字符串JSON转换异常:cn.hutool.json.JSONException: Mismatched hr and body的文章就介绍到这了,更多相关Java字符串JSON转换异常内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Cloud使用Feign实现Form表单提交的示例
本篇文章主要介绍了Spring Cloud使用Feign实现Form表单提交的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-03-03java的SimpleDateFormat线程不安全的几种解决方案
但我们知道SimpleDateFormat是线程不安全的,处理时要特别小心,要加锁或者不能定义为static,要在方法内new出对象,再进行格式化,本文就介绍了几种方法,感兴趣的可以了解一下2021-08-08通过Spring Security魔幻山谷讲解获取认证机制核心原理
这篇文章主要介绍了通过Spring Security魔幻山谷讲解获取认证机制核心原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-04-04
最新评论