【问题标题】:POST request with JSON body in Volley AndroidVolley Android中带有JSON正文的POST请求
【发布时间】:2016-09-24 21:41:03
【问题描述】:

我正在使用 volley 库。我有以下 API url http://example.com/project/contriller/ 并且需要将 json 请求作为正文 {"function":"getList","parameters":{"latitude":"10.0086575","longitude":"76.3187739"},"token":""} 发布给它。

如何使用 Volley 发送?

【问题讨论】:

标签: android android-volley


【解决方案1】:

请检查以下两个选项。

选项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>() { ... });

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2019-12-13
    • 2015-11-28
    • 2020-01-02
    • 2020-12-13
    • 2021-08-11
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多