【问题标题】:Android Volley Post Request - Workaround for JsonArrayRequestAndroid Volley 发布请求 - JsonArrayRequest 的解决方法
【发布时间】:2014-06-23 04:29:38
【问题描述】:

我知道使用 JsonArrayRequest 的 POST 请求在 Volley 中不可用,但我看到这篇文章 here 谈到了添加一个构造函数来处理这个问题。他们的实现是这样的:

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
}

我将如何将它添加为构造函数?上面的问题提到将它放在 Volley Tool Library 中。我将 Volley 作为 .jar 导入,所以我不确定如何添加这样的构造函数,或者这是否是最好的方法。非常感谢任何帮助。

编辑

我已经按照建议创建了以下具有覆盖和构造函数的类。这是课程:

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

在拨打超级电话时,我收到了The constructor JsonArrayRequest(int, String, null, Response.Listener&lt;JSONArray&gt;, Response.ErrorListener) is undefined

我该如何纠正这个问题?

【问题讨论】:

  • 子类 JsonArrayRequest,把构造函数放在那里

标签: android android-volley


【解决方案1】:

创建一个类并扩展 JsonArrayRequest 然后覆盖

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("name", "value");
    return params;
}

并添加一个新的构造函数并在其中调用

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

【讨论】:

  • 感谢您的回答。我创建了一个类来扩展 JsonArrayRequest,添加了 Map 的覆盖,并添加了一个像您的示例一样的构造函数。我已经用代码更新了我的问题。我收到一个错误(上面发布)。
猜你喜欢
  • 2013-08-05
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多