问题描述
我了解使用JsonArrayRequest的POST请求不可用出来抽射箱子,但我看到这个帖子here该谈论加入一个构造函数来处理这个问题。它们的实施是这样的:
I understand that POST requests using JsonArrayRequest are not available out of the box with Volley, but I saw this post here that talked about adding a constructor to handle this. Their implementation was this:
public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(),
listener, errorListener);
}
我将如何去添加此作为构造?以上提到的问题将其凌空工具资源库中我进口排球作为一个.jar,所以我不知道如何添加一个构造函数就是这样,如果这是最好的办法。任何帮助深表AP preciated。
How would I go about adding this as a constructor? The above question mentions placing it in the Volley Tool Library. I imported Volley as a .jar, so I'm not sure how to add a constructor like this, or if this is the best approach. Any help is much appreciated.
修改
我已经创建了覆盖和构造下面的类的建议。这里是类:
I've created the following class with override and constructor as suggested. Here is the class:
public class PostJsonArrayRequest extends JsonArrayRequest {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "value");
return params;
}
public PostJsonArrayRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONArray> listener, ErrorListener errorListener) {
super(Method.POST, url, null, listener, errorListener);
}
}
在该行调用super我越来越构造JsonArrayRequest(整型,字符串,空,Response.Listener&LT; JSONArray&gt;中Response.ErrorListener)未定义
On the line calling super I'm getting The constructor JsonArrayRequest(int, String, null, Response.Listener<JSONArray>, Response.ErrorListener) is undefined
我如何纠正此?
推荐答案
创建一个类并扩展 JsonArrayRequest
然后重写
Create a class and extend JsonArrayRequest
then override
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "value");
return params;
}
和添加一个新的构造,并调用它
and add a new constructor and in it call
super(Method.POST, url, null, listener, errorListener);
或使用此类
public class PostJsonArrayRequest extends JsonRequest<JSONArray> {
/**
* Creates a new request.
* @param url URL to fetch the JSON from
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public PostJsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(Method.POST, url, null, listener, errorListener);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "value");
return params;
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
这篇关于Android的凌空POST请求 - 解决方法JsonArrayRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!