dqteh7347 2016-08-23 13:01 采纳率: 100%
浏览 139

Android ResponseListener如何检索数组数组?

I am developing an app that will be storing to and reading data from MySQL db. At the moment I am able to successfully fetch 1 row but when I am trying to fetch multiple rows it doesn't work. To communicate with my db I am using the volley library.

Here is my code so far:

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条回答 默认 最新

  • doukong1897 2016-08-23 13:18
    关注

    I don't know how your JSON look like. I am posting my JSON and android code. Try to change your code accordingly.

    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 implementation using volley

        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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题