2012-04-24 17 views
-1

私はこれを行う際に多くの混乱を得ています。私がしなければならないのは、JSONPレスポンスをサーバから受け取って解析して表示することです。 AndroidでJSONPレスポンスを解析することは可能ですか?パディング自体が問題になることはありませんあなたJSONP(パディング付きJSON)レスポンスを解析するにはどうすればよいですか?

+4

JSONではなくJSONです。 – Quentin

+0

@Quentin、しかしそれが表示されていますこの操作はJSONP応答をサポートしています。コールバック関数は、 "コールバック" URLクエリパラメータを使用して指定できます。ヘルプページで.. – wolverine

+0

- ブラウザで動作するJSを記述しているわけではないので、JSONPは必要ありません。オプションでサービスを提供できるかどうかは関係ありません。 – Quentin

答えて

0

は、最初に以下のコードを使用してJSONObjectにHTTPストリームから読み込まれた文字列を変換することです。今referece1の値は= String content

そして、ここでは私のHTTP GETのコードです

String reference1=jsonobject.getString("Reference1"); 

:あなたのコードのために、のようなものになるだろう

String url="Your URL Goes Here"; 
DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet(url); 
HttpResponse response; 
try { 
    try 
    { 
     InetAddress i = InetAddress.getByName(url); 
    } catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
    } 
    response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     InputStream instream = entity.getContent(); 
     String result= convertStreamToString(instream); 
     stringContent=new StringContent(result); 
     instream.close(); 
    } 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

以下convertStreamToStringブロックである

private static String convertStreamToString(InputStream is) 
{ 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 
+0

は可能ですURLに特殊文字を渡しますか? – wolverine

+0

レスキュークライアントでurlを送信しているときに応答していますが、応答はjson形式ですが、Eclipseプログラムを通過するとnull値を表示しています。 – wolverine

+0

httpを取得して、問題がある –

0

ありがとう

{ 
    "Reference1":"String content", 
    "Reference2":"String content", 
    "Reference3":"String content", 
    "Reference4":"String content" 

} 

:ここ

は私の応答がどのように見えるです。まともなJSON解析ライブラリはすべてそれをtransperantly扱います。私は個人的にGSONライブラリを使用するように指示します。私はそれを多くのプロジェクトで使用してきたが、不平を言うことは決して見つけられませんでした。

PS:おそらく私はあなたを誤解しているかもしれませんが、パースと言うと、属性の値を取得する必要があり、パディングを保持する必要はないと思われます。

0

私はこれはtutorialがアンドロイドでjsonを解析するの非常に詳細だと思います。これはあなたを助けます。

JSONObject jsonOBJ=new JSONObject(jsonString); 

が続いて各タグを読み取るためにjsonobject.getString("tag")を使用します。私は多くのプロジェクトでやってきた何

+0

本当に "良い答え"他の場所にリンクが含まれています? ([いいえ、そうではありません](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) – Quentin

関連する問題