2012-05-07 9 views
2

私のシステムは、アンドロイドクライアントとwcf-rest-webサービスに基づいています。 私はこのチュートリアルを使用しています:http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx 私は奇妙な問題を抱えています。私は有効なJSON文字列を持っています(オンラインツールでチェックします)。バッファーへの読み込みはうまくいきますが、JSON配列を作成しようとしているときにJSONExceptionが例外変数なしでスローされます(変数はNULLです。 ) excptionをスローライン:有効なJSON文字列がnullの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日午後08時00分00秒"、 "fBuyWindow" 120、 "fNewPrice" 150、 "fOldPrice":400、 "lLatitude":32.021327、 "lLongitude":34.776452、 "nDestinatedPlatform":1、 "nMessageID":1、 "nRange":5、 "nStickingHours":48、 "strDetailedDescription": "N95、הנעלייםשללאונלמסי 、 "strDisplayAddress": "49、"、 "strFullAddress": "49、חולון、ישראל"、 "strImagePath": "http://images.free-extras.com/pics/n /ike_swoosh-703.jpg "、" strItemDescrpition ":" N95לבן "、" strThumbPath ":" http://images.free-extras.com/pics/n/nike_swoosh-703.jpg "、" strTitle "、" fNewPrice ":150、" fOldPrice ":400、" lLatitude ":32.021327": ""、 "dtFromDate": "1899-12-30 20:00:00"、 "fBuyWindow" 、 "lLongitude":34.776452、 "、" nStartingPlatform ":1、" nMessageID ":2、" nRange ":5、" nStickingHours ":48、" strDetailedDescription ":N95、 、 "strThumbPath": ""、 "strFullAddress": ""、 "strTimer"、 "strImagePath": " 、 "LF"、 "fBuyWindow":120、 "fNewPrice":150、 "fOldPrice":400、 "lLatitude":32.021327、 "lLongitude":34.776452、 "、" nStartingPlatform ":1、" nMessageID ":3、" nRange ":5、" nStickingHours ":48、" strDetailedDescription ":N95、 "strThumbPath": ""、 "strTimer": ""、 "strImagePath": "strFullAddress": " {"dtFromDate": "1899-12-30 20:00:00"、 "fBuyWin 1、 "nMessageID":4、 "nRange":5、 "nStickingHours":1、 "nDesignPlatform":1、 "nMessageID":4、 "nRange":400、 "lLatitude":32.021327、 "lLongitude":34.776452、 :48、 "strDetailedDescription": "strDisplayAddress": ""、 "strFullAddress": "49、חולון、ישראל"、 "strImagePath": ""、 "strDetailedDescription": "Nothing、Nothing、Nothing、 「strItemDescrpition」:「N95לבן」、「strThumbPath」:「」、「strTitle」:「英語」、「dtFromDate」:「1899-12-30 20:00:00」、「fBuyWindow "、" nStartingPlatform ":1、" nMessageID ":5、" nRange ":5、" nStickingHours ":120、" fNewPrice ":150、" fOldPrice ":400、" lLatitude ":32.021327、" lLongitude ":34.776452、 48、 "strDetailedDescription": "strDisplayAddress": ""、 "strFullAddress": "49、חולון、ישראל"、 "strImagePath": ""、 "strDetailedDescription": "Nothing、Nothing、 strItemDescrpition ":" N95לבן "、" strThumbPath ":" "、" st rタイトル ":"ノルウェー王国 "}]

何か提案がありますか?

ありがとうございました。

+0

取得している文字列を表示します。 'plates'はJSON配列であり、JSONオブジェクトではありませんか? – Squonk

+0

今あなたが見ることができます – Shahar

+0

私の推測では、文字エンコードの問題です。 JSON文字列にはヘブライ文字がありますが、正しいのですか?あなたのバッファに 'char []'を使用し、 'JSONArray'を作成するときに' new String(buffer) 'を使用することはできません。 'UTF-8'エンコーディングと' ByteStream'を使う必要があります。 – Squonk

答えて

0

次のことを試してみてください。そうしないと、ここJSONArrayを使用して終わる私がやったいくつかのJSON関連のコーディングがあります。

変更するには、このラインは...

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

...次のようにバイト配列を使用するために...

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

あなたはそれが唯一のchar[]で動作するようInputStreamReaderを使用して忘れる必要があります。代わりに... UTF-8でエンコードされた文字列を作成するには、このに使用するストリーム...その後

stream.read(buffer); 

この行を変更...

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

を...読ん

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); 

に役立つことがあります。

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; 
} 
関連する問題