b10*_*101 2 rest android node.js express android-volley
我知道它已经被讨论了十亿次,我已经阅读了几个问题/答案,特别是这个问题/答案似乎是一个很好的例子 - > 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. 所以现在我知道我的POST请求没有正常工作,即使我设置了getParams()我的getHeaders()......
现在,我的猜测是我正在设置 Params 但我正在 fetching 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);
Run Code Online (Sandbox Code Playgroud)
谢谢!
我终于明白了。我继续寻找答案并偶然发现了这个问题/答案链接。
由于我的 REST API 正在寻找req.body变量,因此getParams()无效。为了发送req.body变量getBody(),必须覆盖该方法。
所以我基本上必须做的是:
1)创建一个JSONObject
2) 把我的 key:values 放在 JSONObject 里面
3) 将我的 JSONObject 的内容通过 toString()
4)我的 JsonObjectRequest 里面@Override的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);
Run Code Online (Sandbox Code Playgroud)
感谢@dvdciri 的原始问题和编辑,最终将成为这个答案!
| 归档时间: |
|
| 查看次数: |
2881 次 |
| 最近记录: |