【发布时间】:2015-11-24 17:11:25
【问题描述】:
问题是我不知道如何从好友数组中查看“用户名”:
这是我从 php 接收的 json:
08-30 16:44:08.485
1619-2810/com.gcator E/JSON﹕
{"tag":null,
"success":1,
"error":0,
"friend":{
"username":"Kerrigan",
"latitude":"52.75315",
"longitude":"15.22528"
}
}
{
"tag":null,
"success":1,
"error":0,
"friend":{
"username":"Zahary",
"latitude":"52.72423",
"longitude":"15.24610"
}
}
如您所见,数组也已损坏。如果 php 从 android 收到标签“getfriends”,就是这种情况。我需要在一个数组“朋友”中获得朋友这是代码:
case 'getfriends':
$id = $_POST ['id'];
$response = array();
$user = $db->getUserById($id);
if($user){
$friends = $db->getFriendsByUser($user);
if($friends){
for ($i = 0; $i < count($friends) ; $i++) {
$locationf = $db->getLocationByUser($friends[$i]);
$latitude = $locationf->getLatitude();
$longitude = $locationf->getLongitude();
$name = $friends[$i]->getUsername();
$response = new $response;
$response["friend"]['username'] = $name;
$response["friend"]['latitude'] = $latitude;
$response["friend"]['longitude'] = $longitude;
}
$response["success"]= 1;
echo json_encode($response);
} else {
echo $response = "u dont have any Friends<br>";
}
} else {
echo "dupa";
echo var_dump($friends);
}
,然后在android中使用这个朋友数组来列出“用户名”。
我在收到 json 的地方做了异步任务,但我不知道如何制作循环来提取朋友的用户名并将其放入 listviev,然后单击将重定向到地图活动
private class GetFriends extends AsyncTask<String, Void, JSONObject>
{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FriendList.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Searching Friends ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
SharedPreferences sharedPrefID = getSharedPreferences("userId", Context.MODE_PRIVATE);
String ID = sharedPrefID.getString("KEY_ID", " dupa");
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.getFriends(ID);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
if (json.getString(KEY_SUCCESS) != null) {
String result = json.getString(KEY_SUCCESS);
if(Integer.parseInt(result) == 1){
pDialog.setMessage("Loading View");
pDialog.setTitle("Getting Friends");
}else{
pDialog.dismiss();
Labe.setText("There are no Friends :( Go to site make some !");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
}
}
任何想法/帮助?谢谢 :) 我已经搜索过,但没有适合我的答案。
【问题讨论】:
-
请验证您收到的来自jsonlint.com 的json 响应,然后修改您的php 文件以获得正确的json 响应。
标签: php android arrays json for-loop