Volley - 使用 JSONArrayRequest 发送 POST 请求 [英] Volley - Sending a POST request using JSONArrayRequest
问题描述
我正在使用 Volley 与 API 进行交互.我需要向返回 JSON 数组的服务发送一个 post 请求(带参数).
I'm using Volley to interact with an API. I need to send a post request (with parameters) to a service that returns a JSON Array.
JsonObjectRequest 有一个构造函数,它接受一个方法和一组参数
JsonObjectRequest has a constructor that takes a method and a set of parameters
JsonObjectRequest(int method, java.lang.String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
然而 JSONArrayRequest(我需要的那个)只有一个这样的构造函数
However JSONArrayRequest (the one I need) only has one constructor of the form
JsonArrayRequest(java.lang.String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener)
如何让这个发送带有数据的 POST 请求?
How can I make this send a POST request with data?
推荐答案
他们可能会在稍后添加它,但同时您可以自己添加想要的构造函数:
They're probably going to add it later, but in the meanwhile you can add the wanted constructor yourself:
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
这没有经过测试,但我认为没有理由这不应该起作用,因为实现细节在超类中:JsonRequest
.
This isn't tested, though I see no reason this shouldn't work since the implementation details are in the super class: JsonRequest
.
试试看是否有效.
我叫它!在我回答这个问题后,他们花了将近两年的时间,但 Volley 团队于 2015 年 3 月 19 日将这个构造函数添加到回购中.你猜怎么着?这是 EXACT 语法.
I called it! It took them almost two years after I answered this but the Volley team added this constructor on March 19, 2015 to the repo. Guess what? This is the EXACT syntax.
这篇关于Volley - 使用 JSONArrayRequest 发送 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!