我在將Json數據發送到服務器時遇到問題。我想在UTF-8格式開始時不會出現壞字符的問題。Json解析器錯誤 - 無效的UTF-8起始字節0xa0
我用CharDecoder來替換所有畸形的utf-8字符,這裏是代碼。
// Construct the Decoder
CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE);
utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
// Configure to replace Malformed input with space
utf8Decoder.replaceWith(" ");
// Construct ByteBuffer
ByteBuffer byteBuff = ByteBuffer.wrap(text.getBytes());
try {
// Process the text.
CharBuffer parsed = utf8Decoder.decode(byteBuff);
return new String(parsed.array());
} catch (CharacterCodingException e) {
e.printStackTrace();
}
這不是幫助我。當我查看分析器抱怨的Json發佈數據的列行時,它是一個空格字符。
JSON來發布是
{"body":{"messageSegments":[{"type":"Text","text":"This is a link "},{"type":"Mention","id":"005GGGGGG02g6MMIAZ"},{"type":"Text","text":" ish"}]},"capabilities":{"questionAndAnswers":{"questionTitle":"https:\/\/www.google.co.nz\/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-16"}}}
錯誤是
[{"errorCode":"JSON_PARSER_ERROR","message":"Invalid UTF-8 start byte 0xa0 at [line:1, column:139]"}]
任何線索請。
謝謝
SREE
您正在提取錯誤以解析響應。您可以使用Volley和Gson來調用Rest API。這裏是類似的例子 http://stackoverflow.com/a/37242140/3073945 –
即時通訊使用salesforce移動SDK,所以我沒有自由使用其他NW庫。但Salesforce在內部使用Volley庫。謝謝.. – sha
看到這個鏈接,http://stackoverflow.com/questions/23573994/jackson-jackson-httppost-invalid-utf-8-middle-byte-setting-mime-and-enco – RejoylinLokeshwaran