2012-05-07 97 views
2

我的系統基於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 「:」 נייקקניוןחולון「}]

有什麼建議?

謝謝。

+0

顯示你得到的字符串。你確定'盤子'是一個JSON數組而不是一個JSON對象。 – Squonk

+0

你現在可以看到 – Shahar

+0

我的猜測是這是一個字符編碼問題。從我在JSON字符串中可以看到的有希伯來字符 - 我是否正確?在創建'JSONArray'時使用'char []'作爲你的緩衝區,然後'new String(buffer)'將不起作用。你需要使用'UTF-8'編碼和我認爲的'ByteStream'。 – Squonk

回答

0

請嘗試以下操作。

改變這一行...

char[] buffer = new char[(int)responseEntity.getContentLength()]; 

...使用一個字節數組如下...

byte[] buffer = new byte[(int)responseEntity.getContentLength()]; 

你對使用InputStreamReader因爲它僅與char[]工作給忘了。相反,讀取使用...

stream.read(buffer); 

然後改變這一行

JSONArray plates = new JSONArray(new String(buffer)); 

流......這個創造一個UTF-8編碼的字符串...

JSONArray plates = new JSONArray(new String(buffer, "UTF-8")); 
+0

這很好用!非常感謝! 我現在有一個小問題 - 它似乎在HTTP GET協議上有2000個字符的限制。你有什麼想法如何改變客戶端的另一種協議的限制/工作? – Shahar

+0

@Shahar:我不太明白你的意思。你的意思是GET請求(URL)限制爲2000個字符?或者可下載的金額有限?我知道的下載大小沒有限制 - 例如,我的應用程序下載的文件大小爲幾MB。 – Squonk

0

我建議是這樣的:

String newString = reader.read(buffer); //assuming you only get one line in response, rather than many (add a loop if so) 
JSONArray plates = new JSONArray(newString); 

這可能會有幫助。否則,以下是我使用JSONArray結束的一些與JSON相關的編碼:

public JSONArray getQuestionsJSONFromUrl(String url, List<NameValuePair> params) { 
    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     String jsonData = reader.readLine(); 
     JSONArray jarr = new JSONArray(jsonData); 
     is.close(); 
     return jarr; 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 
    return null; 
}