[英]Sending JSON Post with Body using Android Volley
I am trying to send a JSON Post request using Android Volley library but I dont seem to get the body of the json right and I get undefined body parameters on my web server.我正在尝试使用 Android Volley 库发送 JSON Post 请求,但我似乎没有正确获取 json 的主体,并且在我的 web 服务器上获得未定义的主体参数。 I need the json's parameters body to be a single object "name=someVal&comment=someOtherVal".我需要 json 的参数主体是单个 object“name=someVal&comment=someOtherVal”。 name and comment are the keys and someVal and someOtherVal are the values. name 和 comment 是键,someVal 和 someOtherVal 是值。
String spreadsheetID = "1111111-11111N92RT9h-11111111111111111111111111111";
String url = "https://script.google.com/macros/s/" + spreadsheetID + "/exec";
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSONPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("JSONPost", "Error: " + error.getMessage());
//pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name=someVal&comment=someOtherVal");
//params.put("comment", "someOtherVal");
return params;
}
};
// Add the request to the RequestQueue.
queue.add(jsonObjReq);
} }
I also tried this in the above code but no luck:我也在上面的代码中尝试过这个但没有运气:
params.put("comment", "someOtherVal");
params.put("name", "someVal");
try to put 试着把
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "someOtherVal");
params.put("name", "someVal");
before JsonObjectRequest jsonObjReq ... and change the null value by 在JsonObjectRequest之前jsonObjReq ...并更改null值
new JsonObject(params)
so your code will be 所以你的代码将是
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "someOtherVal");
params.put("name", "someVal");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JsonObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSONPost", response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("JSONPost", "Error: " + error.getMessage());
//pDialog.hide();
}
})
It seems Google Spreadsheet was preferring this format: 谷歌电子表格似乎更喜欢这种格式:
String spreadsheetID = "111111-111111111D746wspoleBbRN92RT9h-111111";
String url = "https://script.google.com/macros/s/" + spreadsheetID + "/exec";
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("name","userAccount.getUsername()");
params.put("comment","userAccount.getPassword()");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
queue.add(sr);
If you need to send a JSON POST then you should dump the http.HEADERS and the http.WIRE and you should probably see .... 如果你需要发送一个JSON POST,你应该转储http.HEADERS和http.WIRE,你应该看到....
"Content-Type: application/json" // among headers
...
{"name":"someVal","comment":"someOtherVal"} // in the POST.body
Find out how to log header and log wire in Volley.... 了解如何在Volley中记录标题和日志线....
Think about testing a POST using CURL on the CLI where adding the -v switch will show you the details. 考虑在CLI上使用CURL测试POST,添加-v开关将显示详细信息。 Then , what works in curl you move that verbatim to your android, http client and it will work. 那么,什么在curl中你可以逐字地移动到你的android,http客户端,它会工作。
This worked for me, I am sending a request to an Express server which uses express.json().这对我有用,我正在向使用 express.json() 的 Express 服务器发送请求。 The object I am sending in the body is called user and looks like this:我在正文中发送的 object 称为用户,如下所示:
{
String: username;
String: password;
}
RequestQueue queue = Volley.newRequestQueue(this);
// user object that we need to send
JSONObject userJson = new JSONObject();
// body of the request
JSONObject body = new JSONObject();
try {
// Put user attributes in a JSONObject
userJson.put("username", "username value");
userJson.put("password", "password value");
// Put user JSONObject inside of another JSONObject which will be the body of the request
body.put("user", userJson);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST,
url,
body,
response -> {
// Handle response
}, e -> {
// handle error
}
);
queue.add(jsonObjectRequest);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.