尝试使用 volly 将 POST 从 android 发送到节点服务器,但服务器上的 json 正文为空
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 {
}
});
这是Android代码
app.post('/hello', function(req,res){
console.log(JSON.stringify(req.body))
})
这是节点js代码
所以我遇到的问题是我将正文打印到控制台,但它一直显示为空。正如您在 post 请求方法中看到的,我 将 'hello as key' 和 'world as value' 放入 jsonobject 中。所以它调用了正确的 post 请求,正文只是空的,无法弄清楚为什么会这样。
编辑-----
我用 wireshark 检查了内容,内容长度为 0,所以我似乎没有发送任何内容。如果我理解正确
您没有在 POST
正文请求中发送任何内容,这就是为什么您在 server-side.
收到空响应的原因
要发送 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;
}
完整示例如下:
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 {
}
});
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 {
}
});
这是Android代码
app.post('/hello', function(req,res){
console.log(JSON.stringify(req.body))
})
这是节点js代码
所以我遇到的问题是我将正文打印到控制台,但它一直显示为空。正如您在 post 请求方法中看到的,我 将 'hello as key' 和 'world as value' 放入 jsonobject 中。所以它调用了正确的 post 请求,正文只是空的,无法弄清楚为什么会这样。
编辑----- 我用 wireshark 检查了内容,内容长度为 0,所以我似乎没有发送任何内容。如果我理解正确
您没有在 POST
正文请求中发送任何内容,这就是为什么您在 server-side.
要发送 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;
}
完整示例如下:
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 {
}
});