在Volley Android中使用JSON正文的POST请求

时间:2016-05-26 18:22:45

标签: android android-volley

我正在使用排球库。我有以下API网址http://example.com/project/contriller/,需要将json请求作为正文{"function":"getList","parameters":{"latitude":"10.0086575","longitude":"76.3187739"},"token":""}发布到其中。

如何使用Volley发送它?

1 个答案:

答案 0 :(得分:2)

请在下面查看两个选项。

<强>选项1

尝试在Map变量中发送数据,如下所示,并将此代码放在上面,使用Post调用请求,如下所示。

        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("function", "getList");
        postParam.put("latitude", "10.0086575");
        postParam.put("token", "");

        new JsonObjectRequest(url, postParam, new Response.Listener<JSONObject>() { ... });

<强>选项2

您可以使用以下内容直接发送JSON。

        final JSONObject jsonData = new JSONObject("{\"function\":\"getList\",\"parameters\":{\"latitude\":\"10.0086575\",\"longitude\":\"76.3187739\"},\"token\":\"\"}");

        new JsonObjectRequest(url, jsonData, new Response.Listener<JSONObject>() { ... });
相关问题