dream07769 2015-08-30 15:27
浏览 51
已采纳

ListView获取JsonArray php

The problem is that i dont know how to view "username" from friend array:

This is the json i'm recieving from php:

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"
 }
}

as You see array is broken too. This is the case if php recieve tag "getfriends" from android. I need to get friends in one array "friend" this is the code:

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

, then use this friend array in android to List "username".

I did Async task where json is recieved but i dont know how to make loop that extract usernames of friends and put it do listviev, then on click will redirect to map activity

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


    }
}

Any ideas/help ? Thanks :) I've searched but there was no good answer for me.

  • 写回答

2条回答 默认 最新

  • dongnao6858 2015-08-30 16:53
    关注

    There is syntax error in your Json..response should be like..

    [
      {
        "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"
        }
      }
    ]
    

    Instead of passing json object pass whole json string to this method there you can put your condition easily... I have changed code below and added your condition...

    Code 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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度