2017-12-21 7 views
0

私はJSONObjectでループしている問題があるようです。forループによるJSONの問題

次のループはtry catch内にあります。私は、返されたJSONをループしようとしていますが、それはonPostExecute内

try { 
    JSONObject jsonObject = new JSONObject(result); 
    // Extract data from json and store into ArrayList as class objects 
    for(int i = 0; i < jsonObject.length(); i++){ 
     JSONObject jD = jsonObject.getJSONObject(i).; 
     DataRecipes recipeData = new DataRecipes(); 
     recipeData.image_url = jD.getString("image_url"); 
     recipeData.title = jD.getString("title"); 
     recipeData.publisher = jD.getString("publisher"); 
     data.add(recipeData); 
    } 
} catch (JSONException e) { 
    Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show(); 
} 

レスポンスJSON

{"count": 30, "recipes": [{"publisher": "Closet Cooking", "f2f_url": "http://food2fork.com/view/35382", "title": "Jalapeno Popper Grilled Cheese Sandwich", "source_url": "http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html", "recipe_id": "35382", "image_url": "http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg", "social_rank": 100.0, "publisher_url": "http://closetcooking.com"}, {"publisher": "The Pioneer Woman", 

コードをスキップします。 変更を加えてもインナーは無視され、キャッチにまっすぐに移動します。

@Override 
protected void onPostExecute(String result) { 
    List<DataRecipes> data = new ArrayList<>(); 

    try { 
     JSONObject jsonObject = new JSONObject(result); 
     JSONArray jsonArrayRecipe = jsonObject.getJSONArray("recipes"); 

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

      JSONObject jD = jsonArrayRecipe.getJSONObject(i); 
      DataRecipes recipeData = new DataRecipes(); 

      recipeData.image_url = jD.getString("image_url"); 
      recipeData.title = jD.getString("title"); 
      recipeData.publisher = jD.getString("publisher"); 

      data.add(recipeData); 
     } 
    } catch (JSONException e) { 
     Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show(); 
    } 
    // Setup and Handover data to recyclerview 
    mRVRecipePrice = findViewById(R.id.jsonText); 
    mAdapter = new AdapterRecipe(Main_Food.this, data); 
    mRVRecipePrice.setAdapter(mAdapter); 
    mRVRecipePrice.setLayoutManager(new LinearLayoutManager(Main_Food.this)); 
} 
+1

を試してアクセスする必要があなたの質問を使用してJSONレスポンスを追加してください。 –

+0

申し訳ありません正しいコードは次のとおりです – mrtmerlin

+0

私のansを確認してください。 –

答えて

1

あなたはレシピの配列最初

この

try { 
     JSONObject jsonObject = new JSONObject(result); 
     JSONArray jsonArrayrecipe = jsonObject .getJSONArray("recipes"); 

     for(int i = 0; i < jsonArrayrecipe .length(); i++){ 
     JSONObject jD = jsonArrayrecipe .getJSONObject(i); 
     DataRecipes recipeData = new DataRecipes(); 
     recipeData.image_url = jD.getString("image_url"); 
     recipeData.title = jD.getString("title"); 
     recipeData.publisher = jD.getString("publisher"); 
     data.add(recipeData); 
    } 
} catch (JSONException e) { 
     Toast.makeText(Main_Food.this, e.toString(), Toast.LENGTH_LONG).show(); 
} 
+0

@mrtmerlinこのコードをAsynTask doInBackground()メソッドに設定してください。 –

関連する問題