user3705697
user3705697

Reputation: 82

Android Volley loading speed slow and freezes progress dialog

Can any one please lemme know how to increase the speed of Volley and remove cache functionality?

I have used onResponse method to load data from web-service that contains more than 500 records. But it displays the data very slowly and freezes the progress dialog till Volley loads all data. Please help. Below is my code -

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

Upvotes: 1

Views: 2365

Answers (1)

Darwind
Darwind

Reputation: 7371

You should use the parseNetworkResponse method for parsing the data and then pass the response to the onResponse method like this:

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

The parseNetworkResponse method will be run on a workerthread instead of the main thread (the UI thread). The onResponse method will be called on the UI thread and that's why you're seeing the "stuttering" in your ProgressDialog.

EDIT:

Seeing as you're using the StringRequest class you can't call the parseNetworkResponse method, unless you sub-class StringRequest. However since you're actually parsing a JSON object from the response, I'd recommend using the JsonRequest class instead, and this one want you to override the parseNetworkResponse.

So your request should look something like this:

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

It's a bit hard to grasp at first, so let me know if there's anything you don't understand :-)

Upvotes: 1

Related Questions