Volley 未收到 http 响应,但邮递员是

Volley not recieving http response but postman is

我将 /getsms GET 请求发送到 API,我在邮递员上得到了预期的结果。然而,当我尝试在 android studio 上通过 volley in java 发出相同的请求时,它只是没有得到 response,我一直在等待,但没有任何反应。

我确定 API 确实 收到了请求,因为预期的更改会在 I send the data associated with the get request.

时发生

所以我不知道为什么它没有得到响应。

Java代码:

    final String url = "http://10.0.2.2:3000/myroute/getsms/"+frm;

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        String frm = response.getString("src_num");
                        String msg = response.getString("msg");
                        int id = response.getInt("id");
                        itemsAdapter.add(frm + ": " + msg);
                        Log.d("Response", response.toString());
                    }
                    catch (Exception err) {
                        Log.d("excpetion", err.toString());
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    );

API代码:

router.get('/getsms/:dest_num', function (req, res) {
  console.log("get oldest unsent sms from db"); 

  let sql = "SELECT * FROM " + table + " WHERE " + "dest_num=" + req.params.dest_num + " AND sent=FALSE " + "ORDER BY id " + "LIMIT 1;";
  console.log(sql);
  db.mycon.query(sql, function (err, result) {
    console.log("Result: " + JSON.stringify(result));
    if(err){
      res.send(err);
    } else {
      console.log("SENT!")
      res.json(result);
    }
  });
});

感谢任何帮助。

更新:所以在筛选日志时我发现了这个:

2020-01-15 22:07:23.481 11880-11880/com.example.sms D/Error.Response: com.android.volley.ParseError: org.json.JSONException: Value [{"id":4,"src_num":"321","dest_num":"1003435365","msg":"first message from server","time":100,"sent":0}] of type org.json.JSONArray cannot be converted to JSONObject

显然已收到响应,但 Volley 在解析时出现问题。我不明白为什么会这样。我没有发现 JSON 字符串有任何问题。这真的足以让它不进入 onResponse 函数吗?

UPDATE2: 显然这确实是问题所在,发送的不是 JSONObject 而是 JSONArray。并且只需要相应地更改数据类型。

所以代码结束工作:

String url = "http://10.0.2.2:3000/myroute/getsms/" + frm;

JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response_arr) {
        try {
            JSONObject response = response_arr.getJSONObject(0);
            String frm = response.getString("src_num");
            String msg = response.getString("msg");
            int id = response.getInt("id");
            itemsAdapter.add(frm + ": " + msg);
        } catch (Exception err) {
            System.out.println(err.toString());
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("Error.Response", error.toString());
    }
});
requestQueue.add(jsonObjectRequest);

感谢评论的帮助:)

您可以尝试下面给出的代码,并将请求添加到 RequestHandler 的新实例的请求队列中。

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                        JSONArray array = new JSONArray(response);  //here is the mistake of parsing which will be removed after it is converted to the json object
                        JSONObject object = array.getJSONObject(0); //-----mistake
                        String frm = object.getString("src_num");
                        String msg = object.getString("msg");
                        int id = object.getInt("id");
                        itemsAdapter.add(frm + ": " + msg);
                        Log.d("Response", response.toString());
                        } catch (JSONException e) {
                           Log.d("excpetion", err.toString());
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
         Log.d("Error.response", err.toString());
            }
        });

        new RequestHandler().addToRequestQueue(stringRequest);

希望对您有所帮助!!