Android: Activity taking too long to display because of web service Http Request

前端 未结 6 556
野性不改
野性不改 2021-01-28 03:17

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 disp

6条回答
  •  不思量自难忘°
    2021-01-28 03:56

        private class getWeather extends AsyncTask {
    
            ProgressDialog dialog = null;
    
            protected void onPreExecute () {
                dialog = ProgressDialog.show(CLASS.this, "", 
                            "Loading. Please wait...", true);
            }
    
            @Override
            protected Cursor doInBackground(Context... params) {
                WeatherSet set = getWeatherCondition("New York, NY");
                return null;
            }
    
            protected void onPostExecute(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.

提交回复
热议问题