2017-10-09 24 views
0

我是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中试了同样的数据,我得到的回应。

+0

你是否尝试登录onErrorResponse方法?你有一些stackTrace日志? – tebitoq

+0

你有没有找到onErrorResponse()? –

+0

我解决了它,但它有点奇怪。内容长度是api期待的问题96.但是我发送了124.但是,124这是邮递员的工作。有人可以解释原因吗? – Stackover67

回答

0

将日志放入 onResponse(JSONObject响应)方法 Log.d(“Response”,response.toString()); (VolleyError error)方法 Log.e(“Error”,error.getMessage());和 onErrorResponse(VolleyError error)。 然后检查logcat,你可以找到出了什么问题......

+0

解决了它。但它有点奇怪。内容长度是问题。该api期待96 ..但我发送124.但与124它是在邮递员工作。有人可以解释原因吗? – Stackover67

+0

可能会将JsonObjectRequest更改为StringRequest将解决您的问题.. –