我正在尝试从以下https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json
下载数据这是我下载数据的Android代码
public class 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);
}
}
}
当我尝试检查下载的数据时,会显示重复的结果和 有时甚至是奇怪的结果,
打印时的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
然后我想也许是因为我没有清除两个arraylists 这是我为两个
添加clear命令时的结果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
请提前告知,为什么会发生这种情况。
答案 0 :(得分:1)
您不需要字段。
您需要单独的成分和步骤每个食谱。
您可能还想查看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);
}
建议:
答案 1 :(得分:0)
这两行需要进入你的循环:
private List<Ingredients> tIngredients = new ArrayList<>();
private List<Steps> tSteps = new ArrayList<>();
另外,你的第二个循环有一个额外的j++
删除它。
答案 2 :(得分:0)
在第二个循环中的doinbackground方法中,在for循环j ++中将j变量增加两倍,然后在循环j ++中增加。除非你想要skipp成分,否则你应该删除循环中的j ++