2017-12-07 12 views
0

私はHttpURLConnectionを使用してPOSTリクエストからの応答を次ていますAndroidでポストリクエストレスポンスから特定のjsonキー値を取得するにはどうすればよいですか?

POSTリクエストの応答:

{ 
    "LatestData": [{ 
     "ExtraData": null, 
     "ID": 0, 
     "season": false, 
     "latest": 0, 
     "url": "http://www.awebsite.com/images/12.jpg" 
    }] 
} 

がどのようにURLの値を取得するには?私は、次の試みたが、Androidのメーカーは私にエラーを与える続ける:

String newURL = sb.getJSONObject("LatestData").getString("url"); 
String newURL = sb.getJSONArray("LatestData").getJSONObject(0).getString("url"); 

のAndroid Studioのエラー:

error: cannot find symbol method getJSONObject(String) 
error: cannot find symbol method getJSONArray(String) 

は、君たちは私がURLの値を取得し、私はにインポートする必要がどのようなライブラリを知らせ助けることができますgetsonObjectが働くので、アンドロイドスタジオのおかげ

アンドロイドコード:?

if (myURLConnection.getResponseCode() == 200) { 
       br = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream(), "utf-8")); 
       while (true) { 
        line = br.readLine(); 
        if (line != null) { 
         sb.append(line + "\n"); 


         //String newURL = sb.getJSONObject("LatestData").getString("url"); 
         String newURL =sb.getJSONArray("LatestData").getJSONObject(0).getString("url"); 
         mWebView.loadUrl("javascript:MyFunction('" +newURL + "');"); 
        } else { 
         br.close(); 
         return sb.toString(); 
        } 
       } 
      } 
+0

まず、sbをJSONObjectに変換する必要があります –

答えて

1

あなたがアクセスプロパティにJSONObjectにSBを変換する必要が

try { 
    JSONArray jsonArray = jsonResponse.getJSONArray("LatestData"); 
    if (jsonArray != null && jsonArray.length() > 0) { 
     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject dataOjbect = jsonArray.getJSONObject(i); 
      String url = dataOjbect.getString("url"); 
     } 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

ありがとうございます返信用ですNongthonbam Tonthoiはjsonobjectに変換するための最初の提案もしました:JSONObject jsonResponse = new JSONObject(new String(sb)); 文字列newURL = jsonResponse.getJSONArray( "LatestData")。getJSONObject(0).getString( "url"); – user1788736

1
その後、

JSONOjbect jsonResponse = new JSONObject(new String(sb)); 

とを::

0

あなたのアプローチは、このようにする必要があります。

try { 
     List<String> urlist = new ArrayList<>(); 
     JSONObject jsonObject = new JSONObject(sb.toString()); 
     JSONArray jsonArray = jsonObject.getJSONArray("LatestData"); 
     for (int count = 0; count<jsonArray.length(); count++){ 
      JSONObject jsonInner = jsonArray.getJSONObject(count); 
      String url = jsonInner.getString("url"); 
      //store your url in some list 
      urlist.add(url); 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
0

あなたはこれを試すことができます

String url = loadJSONArray(sb)..getJSONObject(0).getString("url"); 
0

loadJSONArrayを使用して試してみよう:

私は非常に..あなたはこの記事に従うことができGSONでレトロフィットを使用するように推奨しています。また
StringBuilder sb = new StringBuilder(); 
line = br.readLine(); 
while(line != null){ 
    sb.append(line); 
} 
// if it's fail here then this means there is an issue in parsing JSONObject 
JSONObject jsonObj = new JSONObject(sb.toString());   
JSONArray latestData = jsonObj.getJSONArray("LatestData"); 
JSONObject jsonObject = latestData.getJSONObject(0); 
String url = jsonObject.getString("url"); 

http://www.vogella.com/tutorials/Retrofit/article.html

関連する問題