ListView获取JsonArray php

ListView获取JsonArray php,php,android,arrays,json,for-loop,Php,Android,Arrays,Json,For Loop,问题是我不知道如何从朋友数组中查看“用户名”: 这是我从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":

问题是我不知道如何从朋友数组中查看“用户名”:

这是我从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”,就会出现这种情况。我需要在一个数组中找到朋友“friend”这是代码:

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);
        }
案例“getfriends”:
$id=$_POST['id'];
$response=array();
$user=$db->getUserById($id);
如果($user){
$friends=$db->getFriendsByUser($user);
如果($朋友){
对于($i=0;$igetLocationByUser($friends[$i]);
$latitude=$locationf->getLatitude();
$longitude=$locationf->getLongitude();
$name=$friends[$i]->getUsername();
$response=新的$response;
$response[“friend”][“username”]=$name;
$response[“friend”][“latitude”]=$latitude;
$response[“friend”][“longitude”]=$longitude;
}
$response[“success”]=1;
echo json_编码($response);
}否则{
echo$response=“你没有任何朋友
”; } }否则{ 回声“杜帕”; echo var_dump($friends); }
,然后在android中使用此好友数组列出“用户名”

我做了一个接收json的异步任务,但我不知道如何做一个循环来提取朋友的用户名并将其放入listviev,然后单击将重定向到map活动

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();


    }
}
私有类GetFriends扩展异步任务
{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(FriendList.this);
pDialog.setTitle(“联系服务器”);
setMessage(“搜索朋友…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
SharedReferences SharedReferences=GetSharedReferences(“userId”,Context.MODE\u PRIVATE);
String ID=sharedPrefID.getString(“KEY_ID”,“dupa”);
UserFunctions userFunction=新的UserFunctions();
JSONObject json=userFunction.getFriends(ID);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
试一试{
if(json.getString(KEY_SUCCESS)!=null){
String result=json.getString(KEY_SUCCESS);
if(Integer.parseInt(result)==1){
pDialog.setMessage(“加载视图”);
pDialog.setTitle(“交朋友”);
}否则{
pDialog.disclose();
Labe.setText(“没有朋友:(去网站做一些吧!”);
}
}
}捕获(JSONException e){
e、 printStackTrace();
}
pDialog.disclose();
}
}

有什么想法/帮助吗?谢谢:)我已经搜索过了,但没有找到适合我的答案

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"
    }
  }
]
不需要传递json对象,而是将整个json字符串传递给此方法,您可以轻松地设置条件。。。 我已经更改了下面的代码并添加了您的条件

邮政编码:

@Override
 protected void onPostExecute(String json){
   try {
    JSONObject root = new JSONObject(json);
    String result = root.getString("success");
    JSONArray user_array = root.getJSONArray("users");
        for (int i = 0; i < user_array.length(); i++) {
            JSONObject jsonObj = (JSONObject) user_array.getJSONObject(i);
              if(Integer.parseInt(result) == 1){
                pDialog.setMessage("Loading View");
                pDialog.setTitle("Getting Friends");
                String user = jsonObj.getString("username");
              }else{
                pDialog.dismiss();
                Labe.setText("There are no Friends :( Go to site make some !");
              }
        }
   }catch(JSONException e) {
     e.printStackTrace();
   }
 }
@覆盖
受保护的void onPostExecute(字符串json){
试一试{
JSONObject root=新的JSONObject(json);
字符串结果=root.getString(“成功”);
JSONArray user_array=root.getJSONArray(“用户”);
对于(int i=0;i
您的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"
    }
  }
]
不需要传递json对象,而是将整个json字符串传递给此方法,您可以轻松地设置条件。。。 我已经更改了下面的代码并添加了您的条件

邮政编码:

@Override
 protected void onPostExecute(String json){
   try {
    JSONObject root = new JSONObject(json);
    String result = root.getString("success");
    JSONArray user_array = root.getJSONArray("users");
        for (int i = 0; i < user_array.length(); i++) {
            JSONObject jsonObj = (JSONObject) user_array.getJSONObject(i);
              if(Integer.parseInt(result) == 1){
                pDialog.setMessage("Loading View");
                pDialog.setTitle("Getting Friends");
                String user = jsonObj.getString("username");
              }else{
                pDialog.dismiss();
                Labe.setText("There are no Friends :( Go to site make some !");
              }
        }
   }catch(JSONException e) {
     e.printStackTrace();
   }
 }
@覆盖
受保护的void onPostExecute(字符串json){
试一试{
JSONObject root=新的JSONObject(json);
字符串结果=root.getString(“成功”);
JSONArray user_array=root.getJSONArray(“用户”);
对于(int i=0;i
如果您好奇,这是我的解决方案;):

@覆盖
受保护的void onPostExecute(JSONObject json){
试一试{
if(json.getString(KEY_SUCCESS)!=null){
String result=json.getString(KEY_SUCCESS);
if(Integer.parseInt(result)==1){
pDialog.setMessage(“加载视图”);
pDialog.setTitle(“交朋友”);
//JSONArray root=新的JSONArray(json);
JSONArray friend=json.getJSONArray(“用户”);
列表项=新建ArrayList();
for(int i=0;i