[英]Sending JSON Post with Body using Android Volley
我正在嘗試使用 Android Volley 庫發送 JSON Post 請求,但我似乎沒有正確獲取 json 的主體,並且在我的 web 服務器上獲得未定義的主體參數。 我需要 json 的參數主體是單個 object“name=someVal&comment=someOtherVal”。 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);
}
我也在上面的代碼中嘗試過這個但沒有運氣:
params.put("comment", "someOtherVal");
params.put("name", "someVal");
試着把
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "someOtherVal");
params.put("name", "someVal");
在JsonObjectRequest之前jsonObjReq ...並更改null值
new JsonObject(params)
所以你的代碼將是
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();
}
})
谷歌電子表格似乎更喜歡這種格式:
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);
如果你需要發送一個JSON POST,你應該轉儲http.HEADERS和http.WIRE,你應該看到....
"Content-Type: application/json" // among headers
...
{"name":"someVal","comment":"someOtherVal"} // in the POST.body
了解如何在Volley中記錄標題和日志線....
考慮在CLI上使用CURL測試POST,添加-v開關將顯示詳細信息。 那么,什么在curl中你可以逐字地移動到你的android,http客戶端,它會工作。
這對我有用,我正在向使用 express.json() 的 Express 服務器發送請求。 我在正文中發送的 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.