【问题标题】:Volley not recieving http response but postman isVolley 没有收到 http 响应,但邮递员是
【发布时间】:2020-01-15 19:16:22
【问题描述】:

我将 /getsms GET 请求发送到 API,然后我在邮递员上得到了预期的结果。但是,当我尝试通过 volley in javaandroid studio 上发出相同的请求时,它只是没有得到 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);

感谢cmets的帮助:)

【问题讨论】:

  • 你在 java 代码中的哪里“发送与请求相关的数据”?
  • 我指的数据是附加到url的“frm”。我使用它作为参数在 API 代码中进行查询。
  • 是的,但是当你执行这个java代码时你看到后端的变化了吗?还是仅当您从邮递员寄出时?
  • 是的,API 打印查询结果和“SENT!”控制台中的消息。所以是的,它确实执行了后端代码。

标签: java android node.js api android-volley


【解决方案1】:

您可以尝试下面给出的代码,并将请求添加到新的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);

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2018-12-12
    • 1970-01-01
    • 2014-10-07
    • 2020-08-12
    • 2023-03-23
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多