[英]Trying to send POST from android to node server using volly but the json body is empty on the server
private void postRequest() {
// Request a string response from the provided URL.
// Instantiate the RequestQueue.
String url = "http://10.0.0.9:3000/hello";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
Log.i("*********", "******");
response.put("Hello", "World");
}catch(JSONException e){
Log.i("JSONERROR", e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
Log.i("*********", error.toString());
}
});
jsonObjectRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 1000;
}
@Override
public int getCurrentRetryCount() {
return 1000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
This is the Android code这是安卓代码
app.post('/hello', function(req,res){
console.log(JSON.stringify(req.body))
})
This is the node js code这是节点js代码
So the problem im having is that im printing the body to the console but it keeps showing up empty.所以我遇到的问题是我将正文打印到控制台,但它一直显示为空。 As you can see in the postRequest method ive put 'hello as key' and 'world as value' in the jsonobject.
正如你在 postRequest 方法中看到的,我在 jsonobject 中放置了“hello as key”和“world as value” 。 So it is calling the correct post request, the body is just empty and cant figure out why that is.
所以它正在调用正确的发布请求,正文只是空的,无法弄清楚为什么会这样。
Edit----- Ive checked the content with wireshark and content length is 0 so im not sending anything it seems.编辑-----我用wireshark检查了内容,内容长度为0,所以我似乎没有发送任何内容。 if im understanding this correctly
如果我正确理解这一点
You are not sending anything inside of your POST
body request, that's why you are getting an empty response at your server-side.您没有在
POST
正文请求中发送任何内容,这就是为什么您在服务器端收到空响应的原因。
For sending POST
parameters, you will need to override getParams()
method:要发送
POST
参数,您需要覆盖getParams()
方法:
@Override
protected Map<String, String> getParams() {
Map<String, String> map = new HashMap<String, String>();
map.put("param1", "hello");
map.put("param2", "This is a post request");
return map;
}
Here is the complete example:这是完整的示例:
private void postRequest() {
// Request a string response from the provided URL.
// Instantiate the RequestQueue.
String url = "http://10.0.0.9:3000/hello";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
Log.i("*********", "******");
response.put("Hello", "World");
}catch(JSONException e){
Log.i("JSONERROR", e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
Log.i("*********", error.toString());
}
}) { // HERE TO ADD POST PARAMETERS
@Override
protected Map<String, String> getParams() {
Map<String, String> map = new HashMap<String, String>();
map.put("param1", "hello");
map.put("param2", "This is a post request");
return map;
}
};
jsonObjectRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 1000;
}
@Override
public int getCurrentRetryCount() {
return 1000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.