Skip to content Skip to sidebar Skip to footer

Android: Activity Taking Too Long To Display Because Of Web Service Http Request

One of my activities make a http request to a webservice to get some weather data when I start the application. The issue that the activity will take 3-4 seconds to display because

Solution 1:

The time for your response is unpredictable - your network connection can be very poor and take seconds to transfer a few bytes. So the correct way to do this ( as you propose ) is to use thread. In our case android provides very useful class to handle this situations - AsynTask. After you read the docs you will notice that it has 3 very powerful methods that can help you

  1. onPreExecuteruns in the ui thread - very helpful to show some spinner or some progress indicator to show the user that you are doing some work in background
  2. doInBackgroundruns in background - do your background work here
  3. onPostExecuteruns in the ui thread- when your are done with your background work hide the progress and update the gui with the newly received data.

Solution 2:

privateclassgetWeatherextendsAsyncTask<Context, Void, Cursor> {

        ProgressDialog dialog = null;

        protectedvoidonPreExecute () {
            dialog = ProgressDialog.show(CLASS.this, "", 
                        "Loading. Please wait...", true);
        }

        @Override
        protected Cursor doInBackground(Context... params) {
            WeatherSet set = getWeatherCondition("New York, NY");
            returnnull;
        }

        protectedvoidonPostExecute(Cursor c) {
            dialog.dismiss();
        }
    }

Then where you have WeatherSet set = getWeatherCondition("New York, NY"); now, you'll put new getWeather().execute(this);

I suggest reading how the AsyncTask works, and see why this should work. It goes outside the onCreate() method.

Solution 3:

This is regarding AsyncTask, I just want to help understanding the concept, it is really useful:

DownloadFilesTask dft = newDownloadFilesTask(this);
        //Executes the task with the specified parameters
        dft.execute(Void1...);

        ...
        ...
        ...

        dft.cancel(boolean);

privateclassDownloadFilesTaskextendsAsyncTask<Void1, Void2, Void3> {
        //Runs on the UI thread before doInBackground(Void1...)protectedvoidonPreExecute() {

        }
        //runs in BACKGROUNG threatprotectedVoid3doInBackground(Void1... urls) {
            //it can be invoked from doInBackground(Void1...) to publish updates //on the UI thread while doInBackground(Void1...) is still runningpublishProgress(Void2...);
        }
        //Runs on the UI thread after publishProgress(Void2...) is invokedprotectedvoidonProgressUpdate(Void2... progress) {

        }
        //Runs on the UI thread after doInBackground(Void1...) has finishedprotectedvoidonPostExecute(Void3) {

        }
        //runs in UI threat after cancel(boolean) is invoked and //doInBackground(Void1...) has finishedprotectedvoidonCancelled(Void3) {

        }
}

Solution 4:

You can use AsynchTask class for your web service.You can write your time consuming task in on doInBackground.Also you can use a progress Dialog. Here You can see how to work with AsynchTask.You can also update your UI while web service is parsing without waiting for the complete parsing using onPostUpdate method.

Solution 5:

The response time is normal. Don't worry. Make it a point to run the web-service call in a separate thread.

Regarding the white screen, as soon as you start the web service call, fire a ProgressDialog box. This will run till you receive the response. As soon as you receive the response, dismiss the progressDialog box and start the new activity where you can display the result.

Use the following URLs for reference

http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

http://thedevelopersinfo.wordpress.com/2009/10/16/showing-progressdialog-in-android-activity/

I have implemented the idea I'm giving you and it works perfectly.

Hope I was of some help

Post a Comment for "Android: Activity Taking Too Long To Display Because Of Web Service Http Request"