我想從以下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
請提出爲什麼會發生這種情況,提前致謝。
爲什麼你使用一個AsyncTask使用OkHTTP? –