ListView获取JsonArray php

时间:2015-08-30 15:27:28

标签: php android arrays json for-loop

问题是我不知道如何查看"用户名"来自friend array:

这是我从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收到标签" getfriends"来自android。我需要在一个阵列中结交朋友"朋友"这是代码:

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中使用这个朋友数组来列出&#34;用户名&#34;。

我做了异步任务,其中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();


    }
}

任何想法/帮助?谢谢:)我已经搜索过,但对我来说没有好的答案。

2 个答案:

答案 0 :(得分:0)

你的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字符串传递给此方法,而是可以轻松地处理您的条件...   我在下面更改了代码并添加了您的条件......

代码onPost:

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

答案 1 :(得分:0)

如果你好奇这是我的解决方案;):

@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");


                    //JSONArray root = new JSONArray(json);
                    JSONArray friend = json.getJSONArray("users");
                    List<String> item = new ArrayList<String>();

                    for (int i = 0; i < friend.length(); i++) {
                        JSONObject att = (JSONObject) friend.getJSONObject(i);

                        String res = att.getString("username");
                        item.add(res);



                    }



                    SharedPreferences FriendList = getSharedPreferences("FriendList", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = FriendList.edit();
                    edit.putString("KEY_FRIENDS", item.toString());
                    edit.commit();
                } else {

                    pDialog.dismiss();

                }

            }else {

                pDialog.dismiss();

            }

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

        pDialog.dismiss();


    }

返回[Szamus,Zahariasz] 现在我需要得到它并将其放入ListView -.-