使用Volley发送带有JSON数据的POST请求

安东尼:

我想发送一个新JsonObjectRequest请求:

  • 我想接收JSON数据(来自服务器的响应):确定
  • 我想将带有此请求的JSON格式的数据发送到服务器

    JsonObjectRequest request = new JsonObjectRequest(
        Request.Method.POST, "myurl.com", null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                //...
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //...
            }
        })
        {
            @Override
            protected Map<String,String> getParams() {
                // something to do here ??
                return params;
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // something to do here ??
                return params;
            }
        };
    

PS我在项目中也使用GSON库。

shkschneider:

JsonObjectRequest实际上接受JSONObject为身体。

这篇博客文章中

final String url = "some/url";
final JSONObject jsonBody = new JSONObject("{\"type\":\"example\"}");

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

这是源代码和JavaDoc@param jsonRequest):

/**
 * Creates a new request.
 * @param method the HTTP method to use
 * @param url URL to fetch the JSON from
 * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
 *   indicates no parameters will be posted along with request.
 * @param listener Listener to receive the JSON response
 * @param errorListener Error listener, or null to ignore errors.
 */
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONObject> listener, ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                errorListener);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Volley-使用JSONArrayRequest发送POST请求

发送带有数据的请求

如何使用Alamofire 4在Swift 3中发送带有参数和正文的JSON数据的POST请求?

使用Volley使用JSONArray发送POST请求

如何使用Volley发送带有JSON正文的POST请求?

Volley Post请求,在Json数组请求中发送Json对象

如何在带有参数和标头的Android Volley库中使用POST请求?

Curl-使用带有查询参数的JSON的POST请求

使用Guzzle发送带有JSON的POST请求

使用带有JSON负载的puppeteer发出POST请求

如何使用Retrofit发送带有JSON的POST?

Android Volley Post请求在正文中带有json对象,并在String中获取响应

使用带有ajax的post请求将html表单数据发送到php到https://foo.com

发送带有数据的POST请求时出错

使用request.post发送带有证书的请求

使用Guzzlehttp发送带有JSON正文的POST请求

如何使用python通过POST请求将带有元数据(标题,描述)的媒体发送到WordPress REST API

发送带有数据和JSON参数的发布请求

如何使用Python发送带有数据的获取请求?

如何发送带有表中数据的POST请求?

iOS使用NSJSONSerialization在POST请求中发送JSON数据

Android Volley:服务器上接收到带有POST请求发送的String参数的空值

如何使用Swift将带有JSON数据的POST请求发送到外部数据库以在IOS中登录应用程序?

在带有 redux 形式的 react js 中不使用 axios 发送 post 请求(在 post 请求中不发送任何内容)

使用 Volley 在 POST 请求中发送参数

如何在flask-dance上发送带有json数据的post请求?

带有标题和 JSON 正文的 Volley 中的 POST 请求

使用带有 jQuery 的 POST 请求将数据发送到服务器

尝试使用 Spring JPA 为带有外键的 JSON 对象发送 POST 请求时出现重复或空错误