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



谁能告诉我如何提高齐射的速度和删除缓存功能?

我已经使用了onResponse方法从包含超过500条记录的web-service加载数据。但是它显示数据的速度很慢,并且会冻结进度对话框,直到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);

您应该使用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方法将在工作线程而不是主线程(UI线程)上运行。onResponse方法将在UI线程上被调用,这就是为什么你在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.
    }
};

一开始有点难理解,所以如果有什么不明白的,请告诉我:-)

最新更新