1

Below is my code and I get the data from the webservice(remote server) in the doinbackground(). I want to set the data on the UI. Everything works fine. But as I have huge amount of data to be retrieved from the server, the progress bar is showing for a long time. So, I decided to use something like CW endlesss adapter or according to this. It's been giving me grief for few days by now.

1) CW endless adapter: I face a lot of problems in including it as a library project and when I try to run demos, it always shows me ! symbol in red color. When I click on run, it says your project has errors, but there is not even a single clue where does that error occur. Even I am unable to understand the things that have to be done too as those are somewhat difficult for me as a beginner. So, I decided to follow as per the other.

2) In this, I am unable to get on how to use this in my scenario as I am getting the data as a whole after completion of the doInBackground().

Can someone help me on this with relevant code snippets? I would be very thankful for your help.

I am calling this asynctask in onCreate() method.

class LoadAllData extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(GetData.this);
        pDialog.setMessage("Loading Data. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All Data from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("datatobefetched", datadetails));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_ads, "POST", params);
        Log.d("All Products: ", json.toString());
        try {
            // Checking for SUCCESS TAG
            int check = json.getInt(CHECK);
            if (Check == 1) {
                // Data found
                // Getting Array of Data
                dataJsonArray = json.getJSONArray("totaldata");
                // looping through All data
                for (int i = 0; i < dataJsonArray.length(); i++) {
                    JSONObject jobj = dataJsonArray.getJSONObject(i);
                    // Storing each json item in variable
                    String id = jobj.getString("rowid");
                    String product = jobj.getString("data1");
                    String review = c.getString("data2");
                    String imagepath = c.getString("image_url");

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    map.put("id", id);
                    map.put("product", product);
                    map.put("review", review);
                    map.put("imageurl", imagepath);     
                    // adding map to ArrayList
                    productsList.add(map); // ProductList is arrayList.
                }
            }
            else {
                // no data found
            }
        } 
        catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * After completing background task Dismissing the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Updating parsed JSON data into ListView
                // Getting adapter by passing xml data ArrayList
                ListView list = (ListView)findViewById(R.id.list);   
                adapter = new CustomListAdapter(GetAllAds.this, productsList, passedcategory);        
                list.setAdapter(adapter);
            }
        });
    }
}
Community
  • 1
  • 1
Korhan
  • 323
  • 2
  • 4
  • 20

1 Answers1

1

with the code you posted there doesn't seem to have anything that would be taking an awful a lot of time.

I don't believe making an endless list (either using the endless adapter or manually) is the case for you because apparently the web-server you're communicating with does not have the options for page (e.g. when on the URL you specific items per page and page number like this: ?per_page=20&page_no=1) and those type of list only makes sense for such.

Just as a simple test, put those lines before after your code:

long now = System.currentTimeMillis();
JSONObject json = jParser.makeHttpRequest(url_all_ads, "POST", params);
Log.d("Test", "Elapsed time is: " + Long.toString(System.currentTimeMillis() - now));

then you can check on your LogCat how long did it took to make the http request.

edit:

suggested alternative to HashMap<> as I don't believe they are very efficient ways of storing data.

public class MyData(){
    JSONObject data;
    public MyData(JSONObject data){ this.data=data; }
    public String getId(){ return data.getString("rowid"); }
    // repeat for the other info you need, also override the toString to return the data.toString();
}

that way all the JSON parsing will be done at a later point in time and in small batches and your for loop would be as simple as:

 for (int i = 0; i < dataJsonArray.length(); i++) {
     productList.add(new MyData(dataJsonArray.getJSONObject(i));
 }
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks for your time. The progress bar takes minimum 20-30 seconds to get the data. Even I have high speed internet connection. I specified only few keys in my code to make it simple to understand. But usually it has 12 keys to get for each loop. Even I have 300-500 records to show like this. Please help me. It's been haunting me. – Korhan Dec 27 '12 at 14:01
  • The time your dialog stays visible and the time I proposed on the test are two very different times. Could you please do this test, and depending on the result I can suggest you one thing or another. – Budius Dec 27 '12 at 14:04
  • It gives me 9973 and while the dialog disappear and by the time data made visible on the listiview along with images on the listview it takes some more time. – Korhan Dec 27 '12 at 14:18
  • That test proves a bit my point: the http request is taking 10 seconds to complete. That have nothing to do with your code processing the information. That how longe the server that is online somewhere in the world took to reply to your request. You can also include this same test with a different variable name as the first and last lines of the `doInBackground()` just to compare the difference of the whole processing against just the web request. – Budius Dec 27 '12 at 14:23
  • +1 for your time. It is taking now 16k around. But could you please explain me how could the UI updating would be done at the time the data is loading to avoid even that elapsing time? Or even could you tell me at least to avoid this 6k ms difference? I mean the way to load items onscroll of the listview with my arraylist of items above. – Korhan Dec 27 '12 at 15:54
  • the `AsyncTask` have the `onProgressUpdate` method that you could override to pass the first 20 results, and then at the end of the processing you update the rest of the data. Inside your for loop you include `if(i==20) publishProgress (/* data */);`. Furthermore I don't believe that a HashMap is a very efficient data holder, I'll edit my question with a suggestion alternative. – Budius Dec 27 '12 at 16:07
  • try first the `public class MyData()` suggestion and check the times. – Budius Dec 27 '12 at 16:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21776/discussion-between-korhan-and-budius) – Korhan Dec 27 '12 at 16:21
  • Can you please help me on ActionbarSherlock as I have seen you answering on it in one post. – Korhan Dec 28 '12 at 07:04