2

I want to download some contacts saved on my online server and then display them in my activity. I have written a service and I am giving the code snippet from onStartCommand function, where this downloading is being done.

public int onStartCommand(Intent intent, int flags, int startId)
{
    String responseStr = "";
    try
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);          

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("action", "get_contact"));
        nameValuePairs.add(new BasicNameValuePair("uId", uId + ""));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse httpResponse = httpClient.execute(httpPost);

        BufferedReader in = new BufferedReader (new InputStreamReader(httpResponse.getEntity().getContent()));
        responseStr = in.readLine();        
        in.close();
    }
    catch (ClientProtocolException e)
    {
        Log.e("EXCEPTION", "ClientProtocolException");
    }
    catch (IOException e)
    {
        Log.e("EXCEPTION", "IOException");
    }
    this.stopSelf();

    return Service.START_STICKY;
}

But the problem with this code is that it takes too much time to download the data. Kindly suggest me any way to download and display this data in my activity quickly.

Thanks in advance for your help.

Czechnology
  • 14,832
  • 10
  • 62
  • 88

4 Answers4

0

Well, if your server or connection is slow, there is not much you can do with your client code.

But, you should definitely consider compressing the responses from your web server and on the client code, add the header:

Accept-Encoding: gzip

Anirudh
  • 2,524
  • 1
  • 14
  • 14
0

There is no guarantee that you will not experience slow download or timeouts even after you enable compression at the HTTP level. At the end of the day you are dealing with unbounded data set and what works in your development environment may not work in the field with a very slow connection where a user may have 25000 contacts all with very long name.

It might be worth looking at the problem in general, considering whether you really need to load all the data right away. You may want to use some sort of pagination (see this question for example and/or use EndlessAdapter).

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
0

Try using a socket connection instead.

Orlymee
  • 2,349
  • 1
  • 22
  • 24
0

The thing I notice is you are not doing the download as a background task. Unless your service is running as a separate process from your main activity then this code will execute in the main application (UI) thread of the application and will negatively affect your users. I would recommend you move the code to a AsyncTask or thread launched from onStartCommand. I would then utilize a callback from the service to notify the activity that data is available to display. Also as Dmitry recommends, pagination could help you out as well for the actual download duration.

TGRA
  • 194
  • 1
  • 5