Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

java - Volley - Sending a POST request using JSONArrayRequest

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

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) 

How can I make this send a POST request with data?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

EDIT:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...