json数据只获取最后一个值

时间:2014-08-26 21:42:42

标签: android json sqlite

我正在尝试获取JSON数据并将这些数据插入到SQLite数据库中。 JSON网址中有很多数据。但是,我的应用只获得最后一个值。

这是获取JSON数据的代码

protected ArrayList<HashMap<String, String>> doInBackground(Void... voids) {

            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    restaurants = jsonObject.getJSONArray(TAG_RESTAURANTS);

                    for (int i = 0; i < restaurants.length(); i++) {
                        JSONObject c = restaurants.getJSONObject(i);
                        JSONObject restaurant = c.getJSONObject("Restaurant");
                        id = restaurant.getString("id");
                        name = restaurant.getString("name");
                        image = restaurant.getString("image");
                        description = restaurant.getString("description");
                        address = restaurant.getString("address");
                        phone = restaurant.getString("phone");


                        //Inserting json data into SQLite db
                        db.insertIntoRestaurantsTable(id,name,image,description,address,phone);
                        Log.d("Insert:", "Inserting....");


                        HashMap<String, String> jsonRestaurantData = new HashMap<String, String>();
                        jsonRestaurantData.put("id", id);
                        jsonRestaurantData.put("name", name);
                        jsonRestaurantData.put("image", image);
                        jsonRestaurantData.put("description", description);
                        jsonRestaurantData.put("address", address);
                        jsonRestaurantData.put("phone", phone);

                        arraylist.add(jsonRestaurantData);

                        return arraylist;



                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

这是我的JSON示例

{
restaurants: [
{
Restaurant: {
id: "5",
name: "Dummy Name 5",
image: "443366291404304960DSCF6532.JPG",
description: "Dummy Description 5",
address: "Dummy Address 5",
phone: "1234"
}
},
{
Restaurant: {
id: "4",
name: "Dummy Name 4",
image: "867352851406598423IMG_0433.JPG",
description: "Dummy Description 4",
address: "Dummy Description 4",
phone: "01111222"
}
},
{
Restaurant: {
id: "3",
name: "Dummy Name 3",
image: "6065339moon.jpg",
description: "Dummy description 3",
address: "Dummy Description 3",
phone: "092131232"
}
},

调试后,只有以下最新数据插入到SQLite中。所有其他人都离开了。

Restaurant: {
 id: "5",
 name: "Dummy Name 5",
 image: "443366291404304960DSCF6532.JPG",
 description: "Dummy Description 5",
 address: "Dummy Address 5",
 phone: "1234"
 }
},

我的循环获取JSON数据有什么问题? 请帮帮我。

这是我的数据库代码。

public void insertIntoRestaurantsTable(String restaurant_id, String name, String image, String description, String address, String phone )
    {

        SQLiteDatabase db = this.getWritableDatabase();

        insertValues.put("_id",restaurant_id);
        insertValues.put("_name",name);
        insertValues.put("_image",image);
        insertValues.put("_description",description);
        insertValues.put("_address",address);
        insertValues.put("_phone", phone);

        db.insert(RESTAURANT_TABLE_NAME, null, insertValues);

    }

1 个答案:

答案 0 :(得分:2)

忘掉我的评论。你的问题是return语句在for循环内而不是在for循环之外。所以在第一次迭代之后,该方法将返回。你没有得到最后一个元素,但第一个元素。 而不是这个:

         for(){
                try{
                    ...
                    arraylist.add(jsonRestaurantData);

                    return arraylist;

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
          }

你需要这样做:

         for(){
                try{
                    ...
                    arraylist.add(jsonRestaurantData);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
           }
           return arraylist;