【问题标题】:Volley Request not giving any reponse排球请求没有给出任何回应
【发布时间】:2018-03-20 19:58:29
【问题描述】:

我是 Android 新手。我正在弄清楚如何进行网络调用。我正在尝试将标头与JSON 数据一起发送。我尝试调试代码,但它不会去onResponse()。有人可以告诉我我做错了什么。

下面是我的代码。

 String url = "https://api.....";
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("image", "https://i.imgur.com/5KdM.jpg");
        jsonObject.put("subject_id", "Elixabeth");
        jsonObject.put("gallery_name", "MyGallery");
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                if (response != null){

                }
                // do something...
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // do something...
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                final Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json");
                headers.put("app_id", "");
                headers.put("app_key", "");
                headers.put("Content-Length", "124");

                return headers;
            }
        };
        requestQueue.add(jsonObjectRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }

我用相同的数据在 Postman 中尝试了相同的操作,我得到了响应。

【问题讨论】:

  • 您是否尝试记录 onErrorResponse 方法?你有一些 stackTrace 日志吗?
  • 你到达 onErrorResponse() 了吗?
  • 我解决了..但这有点奇怪。 Content-Length 是 api 期望 96 的问题。但我发送的是 124。但是 124 它在邮递员中工作。有人可以解释原因吗?

标签: java android android-volley


【解决方案1】:

把日志放在里面 onResponse(JSONObject response) 方法 Log.d("Response", response.toString()); 并在 onErrorResponse(VolleyError error) 方法 Log.e("Error", error.getMessage());。 然后查看logcat,可以发现哪里出错了……

【讨论】:

  • 解决了..但这有点奇怪。内容长度是问题所在。 api期待96..但我发送了124。但是124它在邮递员中工作。有人可以解释原因吗?
  • 可能将 JsonObjectRequest 更改为 StringRequest 将解决您的问题..
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多