Mar*_*hot 19 java android android-volley
我正在使用Volley与API进行交互.我需要向返回JSON数组的服务发送一个post请求(带参数).
JsonObjectRequest有一个构造函数,它接受一个方法和一组参数
JsonObjectRequest(int method, java.lang.String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
Run Code Online (Sandbox Code Playgroud)
但是JSONArrayRequest(我需要的那个)只有一个表单构造函数
JsonArrayRequest(java.lang.String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener)
Run Code Online (Sandbox Code Playgroud)
如何使用数据发送POST请求?
Ita*_*ski 48
他们可能会在以后添加它,但同时你可以自己添加想要的构造函数:
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
Run Code Online (Sandbox Code Playgroud)
这个没有经过测试,但我认为没有理由不应该工作,因为实现细节在超类中:JsonRequest.
试一试,看看它是否有效.
我打电话给它!在我回答这个问题差不多两年后,他们花了两年时间,但是Volley团队在2015年3月19日将这个构造函数添加到了回购中.你猜怎么着?这是精确的语法.
我很懒,并没有自己构建Volley库(只使用.jar),因此没有源代码...所以在匿名新的JSONArrayRequest我添加了这些函数:
// NO CONSTRUCTOR AVAILABLE FOR POST AND PARAMS FOR JSONARRAY!
// overridden the necessary functions for this
@Override
public byte[] getBody() {
try {
return paramsArray.toString().getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
public int getMethod() {
return Method.POST;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51787 次 |
| 最近记录: |