【问题标题】:Android Volley: POST request - req.body inside of NodeJS REST API emptyAndroid Volley:POST请求-NodeJS REST API中的req.body为空
【发布时间】:2016-01-11 02:05:14
【问题描述】:

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

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

由于我创建了 REST API,我可以看到这个状态码 400 来自哪里,如果 req.body 不包含 mAtt, mDatum, mRID, mVon,它是我的 NodeJS 响应。所以现在我知道我的POST 请求无法正常工作,即使我设置了getParams()getHeaders() ...

现在,我的猜测是我正在设置 Params,但我正在获取 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);

谢谢!

【问题讨论】:

  • 您是否使用 JSON 正文解析 Express 中间件(例如 body-parser)?
  • @mscdex 是的,我正在使用 Express 4 和 body-parser

标签: android node.js rest express android-volley


【解决方案1】:

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

由于我的 REST API 正在寻找 req.body 变量,因此 getParams() 无效。为了发送req.body 变量,getBody() 方法必须被覆盖。

所以我基本上要做的是:

1) 创建一个 JSONObject

2) 将我的 key:values 放入 JSONObject 中

3) 通过toString() 将我的 JSONObject 的内容转换为字符串

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 他最初的问题和编辑,最终将成为这个答案!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2018-04-30
    • 2018-11-26
    • 2015-11-16
    相关资源
    最近更新 更多