排球 - 发送使用JSONArrayRequest POST请求排球、JSONArrayRequest、POST

2023-09-12 03:36:03 作者:我是文科男

我使用抽射使用API​​进行交互。我需要发送POST请求(带参数)返回一个JSON数组的服务。

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.

试一试,看看是否可行。

Try it and see if it works.

我把它叫做!他们花了近两年后,我回答了这一点,但凌空团队的补充此构造3月19日到2015年回购。你猜怎么了?这是确切的语法。

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.

 
精彩推荐
图片推荐