问题描述
我知道已经讨论了十亿次,并且我已经阅读了几个问题/答案,尤其是一个很好的例子-> .因此,现在我尝试重新创建代码并添加我的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);
谢谢!
我终于明白了.我继续寻找答案,偶然发现了这个问题/答案链接./p>
由于我的REST API正在寻找req.body
变量,因此getParams()
无效.为了发送req.body
变量,必须重写getBody()
方法.
所以我基本上要做的是:
1)创建一个JSONObject
2)将我的key:values放入JSONObject
3)通过toString()
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的原始问题和编辑"表示敬意,最终该答案将变成什么!
I know its been discussed like a billion times, and I have read a couple of questions/answers and this one in particular seemed like a good example -> example. So now I have tried to recreate the code and added my getParams()
as well as my getHeaders()
.Awkwardly I get a HTTP Status Code 400:
E/Volley: [303] BasicNetwork.performRequest: Unexpected response code 400 for http://10.0.2.2:3000/classes
Since I have created the REST API I can see where this status code 400 comes from, its my NodeJS response if the req.body
doesn't contain mAtt, mDatum, mRID, mVon
. So now I know that my POST
request is not properly working even tho I set my getParams()
as well as my getHeaders()
...
Now, my guess would be that I'm setting Params but I'm fetching req.body.mAtt, req.body.mDatum , req.body.mRID, req.body.mVon
, that would explain why my NodeJS returns the status code 400. If I fetched req.query.mAtt
I would probably get something back?
Is there a certain method that need to be overridden by me, to actually set the body instead of the query params?
This is what my POST req looks like:
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);
Thank you!
I finally got it. I continued to search for answers and stumbled upon this question/answer link.
Since my REST API is looking for req.body
variables, the getParams()
have no effect. In order to send req.body
variables the getBody()
method has to be overridden.
So what I basically had to do was:
1) create a JSONObject
2) put my key:values inside the JSONObject
3) get the contents of my JSONObject into a String via toString()
4) @Override
the getBody()
method inside my JsonObjectRequest
Done.
So my corrected code looks like this:
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);
Kudos to @dvdciri for his original question and Edits, what would eventually become this answer!
这篇关于Android Volley:POST请求-NodeJS REST API内的req.body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!