Android Volley:POST请求 – NodeJS REST API中的req.body为空

我知道它被讨论了十亿次,而且我已经阅读了几个问题/答案,特别是这个问题似乎是一个很好的例子 。 所以现在我试图重新创build代码,并添加我的getParams()以及我的getHeaders() 。 尴尬我得到一个HTTP状态代码400:

E/Volley: [303] BasicNetwork.performRequest: Unexpected response code 400 for http://10.0.2.2:3000/classes

因为我已经创build了REST API,所以我可以看到这个状态码400来自哪里,如果req.body不包含mAtt, mDatum, mRID, mVon那么我的req.body响应。 所以现在我知道我的POST请求是不正确的工作,即使我设置我的getParams()以及我的getHeaders()

现在,我的猜测是,我设置参数,但我提取req.body.mAtt, req.body.mDatum , req.body.mRID, req.body.mVon ,这将解释为什么我的NodeJS返回状态代码400.如果我提取了req.query.mAtt我可能会回来一些东西?

是否有某种方法需要我重写,实际设置身体而不是查询参数?

这是我的POST请求的样子:

  JsonObjectRequest JOPR = new JsonObjectRequest(Request.Method.POST, myAcitveLessonPOSTUrl, null, new Response.Listener<JSONObject>(){ @Override public void onResponse(JSONObject response) { try { VolleyLog.v("Response:%n %s", response.toString(4)); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); return headers; } @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("mAtt", "+1"); params.put("mDatum", mDatum); params.put("mRID", mRID); params.put("mVon", mVon); return params; } }; requestQ.add(JOPR); 

谢谢!

我终于明白了 我继续寻找答案,并偶然发现这个问题/答案的链接 。

由于我的REST API正在寻找req.bodyvariables,所以getParams()不起作用。 为了发送req.bodyvariables, getBody()方法必须被覆盖。

所以我基本上必须做的是:

1)创build一个JSONObject

2)把我的关键:值在JSONObject内

3)通过toString()获取我的JSONObject的内容到一个String中

4) @Override我的JsonObjectRequest中的getBody()方法

完成。

所以我更正的代码如下所示:

  JSONObject jsonBodyObj = new JSONObject(); try{ jsonBodyObj.put("mAtt", "+1"); jsonBodyObj.put("mDatum",mDatum); jsonBodyObj.put("mRID",mRID); jsonBodyObj.put("mVon",mVon); }catch (JSONException e){ e.printStackTrace(); } final String requestBody = jsonBodyObj.toString(); JsonObjectRequest JOPR = new JsonObjectRequest(Request.Method.POST, myAcitveLessonPOSTUrl, null, new Response.Listener<JSONObject>(){ @Override public void onResponse(JSONObject response) { try { VolleyLog.v("Response:%n %s", response.toString(4)); populateLessonDetails(myActiveLessonURLFiltered); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); return headers; } @Override public byte[] getBody() { try { return requestBody == null ? null : requestBody.getBytes("utf-8"); } catch (UnsupportedEncodingException uee) { VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8"); return null; } } }; requestQ.add(JOPR); 

对于他原来的问题和编辑,@dvdciri的荣誉,最终会成为这个答案!