解決Java字符串JSON轉(zhuǎn)換異常:cn.hutool.json.JSONException:?Mismatched?hr?and?body
報錯起因:
1、在調(diào)用外部api接口,獲取JSON字符串,通過各種JSON文件轉(zhuǎn)對象的時候發(fā)現(xiàn)報錯。
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有轉(zhuǎn)換的異常,百度,谷歌很多方案,只提示了問題是xml轉(zhuǎn)body錯誤。
一般這種問題還是要看你轉(zhuǎn)換報錯信息,最好給日志打印出來,后來通過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 -->
總結(jié): 問題出現(xiàn)在http請求外部接口的時候,有時候,可能存在網(wǎng)絡(luò)抖動,后者過于頻繁的請求接口,導(dǎo)致接口偶發(fā)性的出現(xiàn)504網(wǎng)絡(luò)異常。導(dǎo)致自己需要查找的數(shù)據(jù)沒有查找到,或者解析出來。
最終解決辦法:
通過重試機(jī)制,三次延時調(diào)用接口,提高對調(diào)用接口這種偶發(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(); } } }
總結(jié)
到此這篇關(guān)于解決Java字符串JSON轉(zhuǎn)換異常:cn.hutool.json.JSONException: Mismatched hr and body的文章就介紹到這了,更多相關(guān)Java字符串JSON轉(zhuǎn)換異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring的UnexpectedRollbackException事務(wù)嵌套示例解析
這篇文章主要為大家介紹了spring的UnexpectedRollbackException事務(wù)嵌套示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java System.currentTimeMillis()時間的單位轉(zhuǎn)換與計算方式案例詳解
這篇文章主要介紹了Java System.currentTimeMillis()時間的單位轉(zhuǎn)換與計算方式案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08理解Java注解及Spring的@Autowired是如何實現(xiàn)的
今天通過本文帶領(lǐng)大家學(xué)習(xí)注解的基礎(chǔ)知識,學(xué)習(xí)Spring的@Autowired是怎么實現(xiàn)的,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07如何把spring boot應(yīng)用發(fā)布到Harbor
這篇文章主要介紹了如何把spring boot應(yīng)用發(fā)布到Harbor,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11IDEA的Terminal無法執(zhí)行g(shù)it命令問題
這篇文章主要介紹了IDEA的Terminal無法執(zhí)行g(shù)it命令問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Java中的Comparable接口與Comparator接口區(qū)別解析
文章介紹了Java中的Comparable接口和Comparator接口,Comparable接口定義了一個compareTo方法,用于比較對象的順序,實現(xiàn)Comparable接口的類可以提供自然排序規(guī)則,詳細(xì)介紹了Java中的Comparable接口與Comparator接口區(qū)別,感興趣的朋友一起看看吧2025-02-02