Android ResponseListener如何检索数组数组?

时间:2016-08-23 13:01:54

标签: php android mysql arrays android-volley

我正在开发一个应用程序,它将存储到MySQL db并从中读取数据。目前我能够成功获取1 row,但当我尝试获取多个rows时,它无法正常工作。要与我的db进行通信,我正在使用volley library

到目前为止,这是我的代码:

ResponceListener

private void getDataFromExternalDB(final String startid){
        id = Integer.parseInt(startid);
Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);

                    while (jsonResponse.getJSONObject(""+id) != null){
                       jsonResponse.getJSONObject(""+id);
                        int user_id = jsonResponse.getInt("user_id");
                        String username = jsonResponse.getString("username");

                        Log.d("getDataFromExternalDB","user_id="+user_id + " username="+username);

                        id++;
                    }

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

        DataRequest dataRequest = new DataRequest (startid, responseListener);
        RequestQueue queue = Volley.newRequestQueue(DataActivity.this);
        queue.add(dataRequest );
}

DataRequest.java

 import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class DataRequest extends StringRequest {
    private static final String DATA_REQUEST_URL = "http://192.168.178.17/getData.php";
    private Map<String, String> params;

    public DataRequest (String startid, Response.Listener<String> listener) {
        super(Method.POST, DATA_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("startid", startid);

    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

访问getdata.php

 <?php

    $connect = mysqli_connect("localhost", "root", "pass", "db");

    $startid = $_POST["startid"];   

    function getData() {
        global $connect, $startid;
        $result = $connect->query("SELECT * FROM users WHERE user_id > " . $startid );
        $responses = array();
        $response = array();
        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {

                $response["user_id"] = $row["user_id"]; 
                $response["username"] = $row["username"];


                $responses["" . $row["user_id"]]=$response;
            }
            echo json_encode($responses);
        }else {
            echo "0 results";
        } 

    }

    getData();  

    $connect->close();  

?>

1 个答案:

答案 0 :(得分:0)

我不知道你的JSON是怎样的。我发布了我的JSON和Android代码。尝试相应地更改您的代码。

JSON

{
"grids": [{
    "id": 1,
    "name": "A",
    "row": 0
}, {
    "id": 2,
    "name": "B",
    "row": 0
}, {
    "id": 3,
    "name": "C",
    "row": 0
}, {
    "id": 4,
    "name": "D",
    "row": 18
}, {
    "id": 5,
    "name": "E",
    "row": 18
}, {
    "id": 6,
    "name": "F",
    "row": 18
}]}

使用排球的Android实施

    StringRequest Req = new StringRequest(base + "data.json",
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    if (null == gridList) {
                        gridList = new ArrayList<>();
                    }
                    JSONObject obj = null;
                    try {
                        obj = new JSONObject(response);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    assert obj != null;
                    JSONArray grids = obj.optJSONArray("grids");
                    for (int i = 0; i < grids.length(); i++) {
                        JSONObject post = grids.optJSONObject(i);

                        post.optString("name");
                        post.optInt("row");
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding our request to the queue
    requestQueue.add(Req);