gpt4 book ai didi

android - 当响应包含印地语或其他特殊字符时,SAXParser 失败

转载 作者:太空狗 更新时间:2023-10-29 12:56:50 27 4
gpt4 key购买 nike

我正在使用 SAX 解析器来解析 XML 响应,但它抛出了异常。

ExpatParser$ParseException:(格式不正确)无效 token

有什么解决办法吗?

这是我的代码:

    HttpParams params = new BasicHttpParams();  
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpPost postMethod = new HttpPost(MyRequestURL);

DefaultHttpClient hc = new DefaultHttpClient(params);

postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler <String> res = new BasicResponseHandler();
String response=hc.execute(postMethodURL,res);


ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(response.getBytes("UTF8"));

/* SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();

/* Create a new ContentHandler and apply it to the XML-Reader*/
MyHandler objHandler = new MyHandler();
xr.setContentHandler(objHandler);

InputSource inputSource = new InputSource(byteArrayInputStream);
inputSource.setEncoding("UTF-8");

/* Parse the xml-data from our URL. */

xr.parse(inputSource);

/* Parsing has finished. */

最佳答案

我不完全确定它会解决您的问题,但我会使用其 setEncoding() 方法在 InputSource 上设置字符集。

InputSource inputSource = new InputSource(byteArrayInputStream);
inputSource.setEncoding("UTF-8");

xr.parse(inputSource);

关于android - 当响应包含印地语或其他特殊字符时,SAXParser 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com