Java 为数组中的每个用户查询数据库
我有一个facebook graph响应,它获取一个名称和ID数组: 我需要为第一个数组中返回的每个单独用户查询一次数据库,使用数组中的ID检查该用户的“状态” 我的代码当前正在数据库中查询两次相同的ID,而不是第一个数组中的两个不同ID。我一定是找错地方了,但我想不出来Java 为数组中的每个用户查询数据库,java,android,json,Java,Android,Json,我有一个facebook graph响应,它获取一个名称和ID数组: 我需要为第一个数组中返回的每个单独用户查询一次数据库,使用数组中的ID检查该用户的“状态” 我的代码当前正在数据库中查询两次相同的ID,而不是第一个数组中的两个不同ID。我一定是找错地方了,但我想不出来 06-13 21:08:54.081: D/FBID:(11514): 277976949048048 06-13 21:08:54.081: D/ID:(11514): 1402927803328937 06-13 21:0
06-13 21:08:54.081: D/FBID:(11514): 277976949048048
06-13 21:08:54.081: D/ID:(11514): 1402927803328937
06-13 21:08:54.081: D/NAME:(11514): Spencer Jones
06-13 21:08:54.631: E/JSON Parser(11514): Error parsing data org.json.JSONException: End of input at character 0 of
06-13 21:08:54.631: D/JSON 1:(11514): {"message":"Products found","success":1,"products":[{"from_user":"277976949048048","them_status":"accepted","status":"pending","to_user":"1402874766667901"}]}
06-13 21:08:54.631: D/FBID:(11514): 277976949048048
06-13 21:08:54.631: D/ID:(11514): 1402874766667901
06-13 21:08:54.631: D/NAME:(11514): Jane Dickson
06-13 21:08:55.191: E/JSON Parser(11514): Error parsing data org.json.JSONException: End of input at character 0 of
06-13 21:08:55.191: D/JSON 1:(11514): {"message":"Products found","success":1,"products":[{"from_user":"277976949048048","them_status":"accepted","status":"pending","to_user":"1402874766667901"}]}
日志中有JSON解析错误,但对我来说没有意义,因为它返回了我需要的所有4个变量。唯一的问题是,它会两次使用相同的ID,而不是两个不同的ID
// getting JSON Array from facebook response
products = FriendResponse.getJSONArray("data");
// looping through products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fbid", fbid));
params.add(new BasicNameValuePair("id", id));
Log.d("FBID: ", fbid);
Log.d("ID: ", id);
// getting JSON string from URL
JSONObject json1 = jsonParser1.makeHttpRequest(url_friend_status, "POST", params1);
Log.d("JSON 1: ", json1.toString());
products1 = json1.getJSONArray(TAG_PRODUCTS);
// looping through products1
for (int j = 0; j < products1.length(); j++) {
JSONObject d = products1.getJSONObject(j);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String status = d.getString(TAG_STATUS);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}
}
//从facebook响应获取JSON数组 products=FriendResponse.getJSONArraydata
// looping through products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String status = c.getString(TAG_STATUS);
String id = c.getString(TAG_ID);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fbid", fbid));
params.add(new BasicNameValuePair("id", id));
Log.d("FBID: ", fbid);
Log.d("ID: ", id);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}
由于JSON文件只有一个数组示例:{myJSONDATA:[{}]},[]只是一个数组,您不需要循环两次,应该只循环一次 您可能希望使用处理数据所使用的语言标记问题。这对于仅显示FriendResponse很好,但我需要查询数据库中FriendResponse中的每个ID以查找其“状态”
// looping through products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String status = c.getString(TAG_STATUS);
String id = c.getString(TAG_ID);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fbid", fbid));
params.add(new BasicNameValuePair("id", id));
Log.d("FBID: ", fbid);
Log.d("ID: ", id);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
productsList.add(map);
}