2016-05-19 4 views
0

以内に私は、配列内の配列を持っている私のコードを、トラブルシューティングをしようとしている、と私はそれは、配列とJSONのトラブルシューティングをしようとすると、アレイ

private TextView tvData; 
private ImageView imgtest; 
String ChampionName; 
String ChampionNameInLowerCase; 
String item2; 
String item3; 
String Booked; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tvData = (TextView) findViewById(R.id.tvJsonItem); 
    imgtest = (ImageView) findViewById(R.id.imageView); 
    // http://api.champion.gg/champion/Ekko/ 

    new JSONTask().execute("http://api.champion.gg/champion/Ekko/"); 
} 


public class JSONTask extends AsyncTask<String, String, String> { 
    @Override 
    protected String doInBackground(String... params) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 



     try { 
      URL url = new URL(params[0]); 

      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream stream = connection.getInputStream(); 

      reader = new BufferedReader(new InputStreamReader(stream)); 

      StringBuffer buffer = new StringBuffer(); 

      String line = ""; 

      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      String finalJson = buffer.toString(); 

      JSONArray jsonarray = new JSONArray(finalJson); 

      for (int i = 0; i < jsonarray.length(); i++) { 

       JSONObject finalObject = jsonarray.getJSONObject(i); 

       ChampionName = finalObject.getString("key"); 
       String role = finalObject.getString("role"); 
       String items = finalObject.getString("items"); 

       JSONObject ItemArray = new JSONObject(items); 
       item2 = ItemArray.getString("mostGames"); 
       JSONObject ItemArray2 = new JSONObject(item2); 
       item3 = ItemArray2.getString("items"); 

       JSONArray jsonarray2 = new JSONArray(item3); 

       for (int j=0;j<jsonarray2.length();j++) { 
        JSONObject finalObject2 = jsonarray.getJSONObject(j); 

        Booked = finalObject2.getString("id"); 
       } 

       return ChampionName + role + item3 + Booked; 
       } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     tvData.setText(result); 

     ChampionNameInLowerCase = ChampionName.toLowerCase().replaceAll("'", "");; 

     int id = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ChampionNameInLowerCase+"_square_0", null ,null); 
     imgtest.setImageResource(id); 




    } 

} 
} 
内部のID値のすべてを取得したいです

そして、問題のコードをもっと詳しく説明します。

JSONArray jsonarray2 = new JSONArray(item3); 

       for (int j=0;j<jsonarray2.length();j++) { 
        JSONObject finalObject2 = jsonarray.getJSONObject(j); 

        Booked = finalObject2.getString("id"); 
       } 

       return ChampionName + role + item3 + Booked; 
       } 

直接上記のコードが追加されると、それは私はコードの小さな断片を削除すると、コードがために

enter image description here

+0

はあなたが解析しようとしているJSONレスポンス何??代わり JSONObject finalObject2 = jsonarray.getJSONObject(jsonarray2.length()-1);を使用することができる方法によって、 –

+0

代わりに 'jsonObj.getInt(" id ")'を呼び出しますか? – jayeshsolanki93

+0

コード内にありますが、ここには - http://api.champion.gg/champion/Ekko/ – Kripzy

答えて

0

問題はここにBooked = finalObject2.getString("id");使用Booked = finalObject2.getInt("id");

であり、あなたがfor (int j=0;j<jsonarray2.length();j++)

+0

が整数にそれを変更する表示されない、何のテキストが – Kripzy

+0

は 'JSONObject finalObject2 = jsonarray2.getJSONObject(jsonarray2.length()何@Juliusエイダの答えをしてみてください表示されません-1); ' – yohayco

+0

こんにちは、これは実際には動作しますが、最後のid値だけを返します。 http://i.imgur.com/QKpF8YQ.png – Kripzy

0

を生成するエラーを

org.json.JSONException: No value for id 

を与えますjsonarray2を反復処理してjsonarray2の代わりにjsonarrayにアクセスして、JSONObjectを取得します。

関連する問題