Android Volley加载速度慢并冻结进度对话框

时间:2014-07-01 08:57:16

标签: android android-volley

任何人都可以知道如何提高Volley的速度并删除缓存功能吗?

我使用onResponse方法从包含500多条记录的网络服务加载数据。但它会非常缓慢地显示数据并冻结进度对话框,直到Volley加载所有数据。 请帮忙。 以下是我的代码 -

    C.progressStart(context, "Loading Data...", "Please Wait");
    String tag_string_req = "string_req";
    final List<List<String>> values = new ArrayList<List<String>>();
    datas = new ArrayList<ResultData>();
    String eid = id;
    url = C.EVENT + eid;
    request = new StringRequest(Method.GET, url,
            new Listener<String>() {

                @Override
                public void onResponse(String arg0) {
                    C.progressStop();
                    // TODO Auto-generated method stub
                    JsonParser parent = new JsonParser(arg0);
                    header.setText(parent.getValue("name"));
                    String resp = parent.getValue("data");
                                            -----
                                            -----
                                    }, new ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                    // TODO Auto-generated method stub
                    C.ToastShort(context,
                            "Please check your network connection");
                     C.progressStop();
                }
            });
    queue.add(request);

1 个答案:

答案 0 :(得分:1)

您应该使用parseNetworkResponse方法解析数据,然后将响应传递给onResponse方法,如下所示:

parseNetworkResponse(NetworkResponse response) {
    // Parse the response here.
    // When done parsing the response return it:
    if(success) {
        return Response.success(theParsedResult, HttpHeaderParser.parseCacheHeaders(null));
        // HttpHeaderParser.parseCacheHeaders(null) will not cache the response.
    } else {
        // Something was wrong with the response, so return an error instead.
        return Response.error(new ParseError(new Exception("My custom message about the error)));
    }
}

parseNetworkResponse方法将在workerthread而不是主线程(UI线程)上运行。将在UI线程上调用onResponse方法,这就是为什么您会看到&#34; stuttering&#34;在ProgressDialog

修改

看到您正在使用StringRequest课程,您无法调用parseNetworkResponse方法,除非您将StringRequest分类。但是,由于您实际上正在从回复中解析JSON个对象,因此我建议您使用JsonRequest类,而这个要求您覆盖parseNetworkResponse

所以你的请求应该是这样的:

JsonRequest<List<MyParsedCustomObjects>> request =  JsonRequest<List<MyParsedCustomObjects>>(Request.Method.GET, "my_url", null, new Response.Listener<List<MyParsedCustomObjects>>() {
    @Override
    public void onResponse(List<MyParsedCustomObjects> response) {
        // Populate your list with your parsed result here.
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
}) {
    @Override
    protected Response<List<MyParsedCustomObjects>> parseNetworkResponse(NetworkResponse response) {
        // Handle the response accordingly off the main thread. The return the parsed result.
        return null; // Return your parsed List of objects here - it will be parsed on to the "onResponse" method.
    }
};

起初有点难以理解,所以请告诉我是否有任何你不明白的事情: - )