Sending JSON Post with Body using Android Volley

2020-06-23 05:52发布

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. I need the json's parameters body to be a single object "name=someVal&comment=someOtherVal". name and comment are the keys and someVal and someOtherVal are the values.

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");

3条回答
神经病院院长
2楼-- · 2020-06-23 06:15

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);
查看更多
\"骚年 ilove
3楼-- · 2020-06-23 06:16

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

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();
    }
})
查看更多
霸刀☆藐视天下
4楼-- · 2020-06-23 06:18

If you need to send a JSON POST then you should dump the http.HEADERS and the http.WIRE and you should probably see ....

 "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....

Think about testing a POST using CURL on the CLI where adding the -v switch will show you the details. Then , what works in curl you move that verbatim to your android, http client and it will work.

查看更多
登录 后发表回答