Volley Android中带有JSON正文的POST请求 [英] POST request with JSON body in Volley Android

查看:90
本文介绍了Volley Android中带有JSON正文的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using volley library. I have the following API url http://example.com/project/contriller/ and need to post the json request as body {"function":"getList","parameters":{"latitude":"10.0086575","longitude":"76.3187739"},"token":""}to it.

如何使用Volley进行发送?

How can send it using Volley?

推荐答案

请为此检查以下两个选项.

Please check below two options for that.

选项1

尝试按如下所示在Map变量中发送数据,并将此代码放在正要使用Post调用请求的位置的上方,如下所示.

Try to send data in Map variable as below, and put this code just above you are calling request using Post as below.

        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.

You can use below to send direct JSON.

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

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

这篇关于Volley Android中带有JSON正文的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆