2017-06-20 83 views
0

我想從以下https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.jsonAndroid的 - 解析JSON數據顯示重複的結果

這裏下載的數據是我的Android代碼下載數據

公共類DownloadTask擴展的AsyncTask {

private List<Ingredients> tIngredients = new ArrayList<>(); 
private List<Steps> tSteps = new ArrayList<>(); 

String result; 

@Override 
protected String doInBackground(String... params) { 
    result = ""; 
    OkHttpClient client = new OkHttpClient(); 
    Request request = new Request.Builder().url("https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json").build(); 
    try { 
     result = client.newCall(request).execute().body().string(); 
     Log.i("RESULT", result); 
     JSONArray rootArray = new JSONArray(result); 
     for (int i = 0; i < rootArray.length(); i++) { 
      JSONObject tempObject = rootArray.getJSONObject(i); 
      JSONArray ingredients = tempObject.getJSONArray("ingredients"); 
      for(int j = 0 ; j< ingredients.length(); j++) { 
       JSONObject tempIngredient = ingredients.getJSONObject(j); 
       Ingredients newIngredient = new Ingredients(tempIngredient.getString("quantity"), 
         tempIngredient.getString("measure"), 
         tempIngredient.getString("ingredient")); 
       tIngredients.add(newIngredient); 
       j++; 
      } 
      JSONArray steps = tempObject.getJSONArray("steps"); 
      for (int k = 0; k < steps.length(); k++) { 
       JSONObject tempStep = steps.getJSONObject(k); 
       Steps newStep = new Steps(tempStep.getString("id"), tempStep.getString("shortDescription"), 
         tempStep.getString("description"), tempStep.getString("videoURL")); 
       tSteps.add(newStep); 
      } 
      Recipe newRecipe = new Recipe(tempObject.getString("id"), tempObject.getString("name"), tempObject.getString("servings"), tIngredients, tSteps); 
      MainActivity.mRecipies.add(newRecipe); 

     } 
    } catch (Exception e) { 
     Log.i("TAG", e.getMessage()); 
    } 

    return null; 
} 

@Override 
protected void onPostExecute(String s) { 


    for (int i = 0; i < MainActivity.mRecipies.size(); i++) { 

     Log.i("Recipie Number", String.valueOf(i)); 
     for (int j = 0; j < MainActivity.mRecipies.get(i).getIngredients().size(); j++) { 
      Log.i("Ingre - ", MainActivity.mRecipies.get(i).getIngredients().get(j).getIngredient()); 

     } 
     MainActivity.myList.setAdapter(MainActivity.myAdapter); 
     super.onPostExecute(s); 
    } 
} 

}

當我嘗試檢查下載的數據時,顯示重複的結果和 som etimes甚至奇怪的結果,

打印時logcat的..

06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Recipie Number: 0 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: Mascapone Cheese(room temperature) 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: cream cheese(softened) 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: Bittersweet chocolate (60-70% cacao) 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: large eggs 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: all purpose flour 
06-21 05:16:01.402 26656-26656/com.example.vamshi.baking I/Ingre -: salt 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: sifted cake flour 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: baking powder 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla extract, divided 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: whole milk 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: egg whites 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla,divided 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: large whole eggs 
06-21 05:16:01.403 26656-26656/com.example.vamshi.baking I/Ingre -: heavy cream 
06-21 05:16:01.404 26656-26656/com.example.vamshi.baking I/Recipie Number: 1 
06-21 05:16:01.404 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: Mascapone Cheese(room temperature) 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: cream cheese(softened) 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: Bittersweet chocolate (60-70% cacao) 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: large eggs 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: all purpose flour 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: salt 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: sifted cake flour 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: baking powder 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla extract, divided 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: whole milk 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: egg whites 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla,divided 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: large whole eggs 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: heavy cream 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Recipie Number: 2 
06-21 05:16:01.405 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: Mascapone Cheese(room temperature) 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: cream cheese(softened) 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: Bittersweet chocolate (60-70% cacao) 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: large eggs 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: all purpose flour 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: salt 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: sifted cake flour 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: baking powder 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla extract, divided 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: whole milk 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: egg whites 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla,divided 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: large whole eggs 
06-21 05:16:01.406 26656-26656/com.example.vamshi.baking I/Ingre -: heavy cream 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Recipie Number: 3 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: Mascapone Cheese(room temperature) 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: cream cheese(softened) 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: Bittersweet chocolate (60-70% cacao) 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: large eggs 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: all purpose flour 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: salt 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: sifted cake flour 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: baking powder 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla extract, divided 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: whole milk 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: egg whites 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: Graham Cracker crumbs 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: granulated sugar 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: vanilla,divided 
06-21 05:16:01.407 26656-26656/com.example.vamshi.baking I/Ingre -: large whole eggs 
06-21 05:16:01.408 26656-26656/com.example.vamshi.baking I/Ingre -: heavy cream 

然後我想也許是因爲我不是清除兩個的ArrayList 這是結果,當我添加了明確的命令,兩個

Log.i("RESULT", result); 
     JSONArray rootArray = new JSONArray(result); 
     for (int i = 0; i < rootArray.length(); i++) { 
      tIngredients.clear(); 
      tSteps.clear(); 
      JSONObject tempObject = rootArray.getJSONObject(i); 
      JSONArray ingredients = tempObject.getJSONArray("ingredients"); 

的結果如下

06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Recipie Number: 0 
06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Ingre  -: Graham Cracker crumbs 
06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Ingre  -: granulated sugar 
06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Ingre  -: vanilla,divided 
06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Ingre  -: large whole eggs 
06-21 05:21:50.464 32625-32625/com.example.vamshi.baking I/Ingre  -: heavy cream 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Recipie Number: 1 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: Graham Cracker crumbs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: granulated sugar 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: vanilla,divided 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: large whole eggs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: heavy cream 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Recipie Number: 2 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: Graham Cracker crumbs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: granulated sugar 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: vanilla,divided 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: large whole eggs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: heavy cream 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Recipie Number: 3 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: Graham Cracker crumbs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: granulated sugar 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: vanilla,divided 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: large whole eggs 
06-21 05:21:50.465 32625-32625/com.example.vamshi.baking I/Ingre  -: heavy cream 

請提出爲什麼會發生這種情況,提前致謝。

+1

爲什麼你使用一個AsyncTask使用OkHTTP? –

回答

0

你不需要字段。

你想單獨的配料和步驟每配方

您可能也想看看Why does my ArrayList contain N copies of the last item added to the list?

JSONArray rootArray = new JSONArray(result); 
    // For each Recipe 
    for (int i = 0; i < rootArray.length(); i++) { 
     JSONObject tempObject = rootArray.getJSONObject(i); 
     JSONArray jIngredients = tempObject.getJSONArray("ingredients"); 
     JSONArray jSteps = tempObject.getJSONArray("steps"); 

     // Get the ingredients 
     List<Ingredients> ingredients = new ArrayList<>(); 
     for(int j = 0 ; j< jIngredients.length(); j++) { 
      JSONObject tempIngredient = jIngredients.getJSONObject(j); 
      Ingredients newIngredient = new Ingredients(tempIngredient.getString("quantity"), 
        tempIngredient.getString("measure"), 
        tempIngredient.getString("ingredient")); 
      ingredients.add(newIngredient); 
     } 

     // Get the steps 
     List<Steps> steps = new ArrayList<>(); 
     for (int j = 0; j < jSteps.length(); j++) { 
      JSONObject tempStep = jSteps.getJSONObject(j); 
      Steps newStep = new Steps(tempStep.getString("id"), tempStep.getString("shortDescription"), 
        tempStep.getString("description"), tempStep.getString("videoURL")); 
      steps.add(newStep); 
     } 

     // Create the recipe 
     Recipe newRecipe = new Recipe(tempObject.getString("id"), tempObject.getString("name"), tempObject.getString("servings"), ingredients, steps); 
     MainActivity.mRecipies.add(newRecipe); 
    } 

建議:

  • 改造
  • GSON
  • 不使用靜態變量在你的Activity類
+0

我試着用你的方法,但現在相同的數據重複,第一recipie的成分重複其餘的 –

+0

你是否閱讀我的答案中的鏈接,它是否適用於您的代碼? –

+0

是的,我讀了你的鏈接,它完全適用於我的代碼,但我使用靜態,因爲有更多的活動取決於mRecipies arraylist。你能解釋一下如何解決這個問題。 –

0

這兩行需要去您的循環內:

private List<Ingredients> tIngredients = new ArrayList<>(); 
private List<Steps> tSteps = new ArrayList<>(); 

而且,你第二循環有一個額外的j++刪除。

0

在第二個循環中的doinbackground方法中,您將循環j ++中的j變量增加兩次一次,然後循環j ++中。除非你想滑雪配料你應該刪除循環內的j ++