2017-12-21 7 views
0

JSONObject를 통해 반복되는 문제가있는 것 같습니다.JSON을 통한 for 루프 관련 문제

다음 루프는 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() 메소드로 설정하십시오. –