0

I have a android application, where i extract data from the multiple urls and save then as arraylist of string. It works fine, but for fetching data from 13 urls, it takes close to 15-20 sec. Where as fetching the data from same set of urls take 3-4 sec in same app built using phonegap. Here is the code below.

        @Override
        protected String doInBackground(String... params) {
            client = new DefaultHttpClient();
            for(int i=0;i<url.size();i++)
            {
                get = new HttpGet(url.get(i));
                try {
                    response = client.execute(get);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                entity = response.getEntity();
                InputStream is = null;
                try {
                    is = entity.getContent();
                } catch (IllegalStateException e) {         
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is));
                StringBuffer buffer = new StringBuffer();
                String line = null;
                do {
                    try {
                        line = reader.readLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    buffer.append(line);
                } while (line != null);
                String str = buffer.toString();
                param.add(str);             
            }
            return null;
        }

Could anyone please suggest how i can speed this execution and reduce the extraction time.

bharath
  • 953
  • 4
  • 17
  • 30

1 Answers1

0

You could try starting a separate thread for each iteration from the for loop. Smth like this :

for(int i = 0; i < url.size(); i++){ 
    //start thread that gets data from url and adds it to the list
}
sabadow
  • 5,095
  • 3
  • 34
  • 51
Dan Riza
  • 397
  • 3
  • 11