我的系統基於Android客戶端和WCF - 休息 - Web服務。 我正在使用本教程:http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx 我有一個奇怪的問題。我有一個有效的JSON字符串(使用在線工具進行檢查),緩衝區讀取順利,但是當我嘗試創建JSON數組時,它會拋出JSONException,但沒有異常變量(該變量爲NULL - 在我之前從未發生過) 拋出的excption行:有效的JSON字符串拋出JSONException與空異常變量
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
JSONArray plates = new JSONArray(new String(buffer));
最後一行拋出異常。
來自該服務的文本是: [{「dtFromDate」:「1899-12-30 20:00:00」,「fBuyWindow」:120,「fNewPrice」:150,「fOldPrice」:400, 「lLatitude」:32.021327,「lLitude」:34.776452,「nDestinatedPlatform」:1,「nMessageID」:1,「nRange」:5,「nStickingHours」:48,「strDetailedDescription」:「 ,בהנחהמטורפת 「」 strDisplayAddress。 「:」 חנקין49,חולון 「 」strFullAddress「: 」חנקין49,חולון,ישראל「, 」strImagePath「:」 http://images.free-extras.com/pics/n /nike_swoosh-703.jpg","strItemDescrpition 「:」 נעלינייקדגםN95לבן 「 」strThumbPath「: 」http://images.free-extras.com/pics/n/nike_swoosh-703.jpg「,」 strTitle 「:」 נייקקניוןחולון 「},{」 dtFromDate 「:」 1899-12-30 20" 時00分○○秒, 「fBuyWindow」:120, 「fNewPrice」:150, 「fOldPrice」:400, 「lLatitude」:32.021327 「lLongitude」:34.776452, 「nDestinatedPlatform」:1,「nMessageID」:2,「nRange」:5,「nStickingHours」:48,「strDetailedDescription」:「下一個N95,下一個,下一個。」,「strDisplayAddress」:「 ,「strFullAddress」:「חנקין49,חולון,ישראל」,「strImagePath」:「」,「strItemDescrpition」:「נעלינייקנגםN95לבן」,「strThumbPath」:「」,「strTitle」:「נייקקניוןחולון」} ,{ 「dtFromDate」: 「1899-12-30 20時00分○○秒」, 「fBuyWindow」:120, 「fNewPrice」:150, 「fOldPrice」:400, 「lLatitude」:32.021327, 「lLongitude」:34.776452,」 nDestinatedPlatform「:1,」nMessageID「:3,」nRange「:5,」nStickingHours「:48,」strDetailedDescription「:」Nand, 「strFullAddress」:「חנקין49,חולון,ישראל」,「strImagePath」:「」,「strItemDescrpition」:「נעלינייקנגםN95לבן」,「strThumbPath」:「」,「strTitle」:「נייקקניוןחולון」}, {「dtFromDate」:「1899-12-30 20:00:00」,「fBuyWin陶氏 「120」,fNewPrice 「:150,」 fOldPrice 「:400,」 lLatitude 「:32.021327,」 lLongitude 「:34.776452,」 nDestinatedPlatform 「:1,」 nMessageID 「:4」,nRange 「:5」,nStickingHours」 :48,「strDetailedDescription」:「,」strDisplayAddress「:」「,」strFullAddress「:」חנקין49,חולון,ישראל「,」strImagePath「:」「, 「strItemDescrpition」: 「נעלינייקדגםN95לבן」, 「strThumbPath」: 「」, 「strTitle」: 「נייקקניוןחולון」},{ 「dtFromDate」: 「1899-12-30 20時00分00秒」,「fBuyWindow 「:120,」 fNewPrice 「:150,」 fOldPrice 「:400,」 lLatitude 「:32.021327,」 lLongitude 「:34.776452,」 nDestinatedPlatform 「:1,」 nMessageID 「:5」,nRange 「:5」,nStickingHours「: 48,「strDetailedDescription」:「,」strDisplayAddress「:」「,」strFullAddress「:」חנקין49,חולון,ישראל「,」strImagePath「:」「,」strDetailedDescription「 strItemDescrpition「:」נעלינניםקגםN95לבן「,」strThumbPath「:」「,」st rTitle 「:」 נייקקניוןחולון「}]
有什麼建議?
謝謝。
顯示你得到的字符串。你確定'盤子'是一個JSON數組而不是一個JSON對象。 – Squonk
你現在可以看到 – Shahar
我的猜測是這是一個字符編碼問題。從我在JSON字符串中可以看到的有希伯來字符 - 我是否正確?在創建'JSONArray'時使用'char []'作爲你的緩衝區,然後'new String(buffer)'將不起作用。你需要使用'UTF-8'編碼和我認爲的'ByteStream'。 – Squonk