【问题标题】:Android ResponseListener How to retrieve array of arrays?Android ResponseListener 如何检索数组?
【发布时间】:2016-12-30 07:56:53
【问题描述】:

我正在开发一个应用程序,它将存储到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();  

?>

【问题讨论】:

    标签: php android mysql arrays android-volley


    【解决方案1】:

    我不知道你的 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
    }]}
    

    使用 volley 实现 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多