使用Android Volley发送带有Body的JSON帖子

时间:2016-05-08 13:27:36

标签: android json post android-volley

我正在尝试使用Android Volley库发送JSON Post请求,但我似乎没有正确的json的主体,我在我的Web服务器上得到未定义的身体参数。我需要json的参数body作为单个对象" 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");

3 个答案:

答案 0 :(得分:4)

尝试放

log_bin_trust_function_creators

在JsonObjectRequest jsonObjReq之前...并通过

更改空值
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "someOtherVal");
params.put("name", "someVal");

所以你的代码将是

new JsonObject(params)

答案 1 :(得分:2)

Google Spreadsheet似乎更喜欢这种格式:

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

答案 2 :(得分:0)

如果您需要发送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客户端,它会起作用。