2016-05-30 850 views
1

我在將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

+0

您正在提取錯誤以解析響應。您可以使用Volley和Gson來調用Rest API。這裏是類似的例子 http://stackoverflow.com/a/372​​42140/3073945 –

+0

即時通訊使用salesforce移動SDK,所以我沒有自由使用其他NW庫。但Salesforce在內部使用Volley庫。謝謝.. – sha

+0

看到這個鏈接,http://stackoverflow.com/questions/23573994/jackson-jackson-httppost-invalid-utf-8-middle-byte-setting-mime-and-enco – RejoylinLokeshwaran

回答

0

0XA0是Unicode字符 'NO-BREAK SPACE'(U + 00A0)

不是通常的空間爲0x20

視覺難以察覺的差異,但一些框架不喜歡。

相關問題