且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-17 08:31:34

JsonObjectRequest 实际上接受 JSONObject 作为主体.

JsonObjectRequest actually accepts JSONObject as body.

来自这篇博客文章

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