解決Java字符串JSON轉換異常:cn.hutool.json.JSONException:?Mismatched?hr?and?body
報錯起因:
1、在調用外部api接口,獲取JSON字符串,通過各種JSON文件轉對象的時候發(fā)現報錯。
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請求外部接口的時候,有時候,可能存在網絡抖動,后者過于頻繁的請求接口,導致接口偶發(fā)性的出現504網絡異常。導致自己需要查找的數據沒有查找到,或者解析出來。
最終解決辦法:
通過重試機制,三次延時調用接口,提高對調用接口這種偶發(fā)性的問題的解決方案。
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轉換異常內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring的UnexpectedRollbackException事務嵌套示例解析
這篇文章主要為大家介紹了spring的UnexpectedRollbackException事務嵌套示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解
這篇文章主要介紹了Java System.currentTimeMillis()時間的單位轉換與計算方式案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08
理解Java注解及Spring的@Autowired是如何實現的
今天通過本文帶領大家學習注解的基礎知識,學習Spring的@Autowired是怎么實現的,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07
Java中的Comparable接口與Comparator接口區(qū)別解析
文章介紹了Java中的Comparable接口和Comparator接口,Comparable接口定義了一個compareTo方法,用于比較對象的順序,實現Comparable接口的類可以提供自然排序規(guī)則,詳細介紹了Java中的Comparable接口與Comparator接口區(qū)別,感興趣的朋友一起看看吧2025-02-02

