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

前端 未结 6 555
野性不改
野性不改 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 04:01

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

            DownloadFilesTask dft = new DownloadFilesTask(this);
            //Executes the task with the specified parameters
            dft.execute(Void1...);
    
            ...
            ...
            ...
    
            dft.cancel(boolean);
    
    private class DownloadFilesTask extends AsyncTask {
            //Runs on the UI thread before doInBackground(Void1...)
            protected void onPreExecute() {
    
            }
            //runs in BACKGROUNG threat
            protected Void3 doInBackground(Void1... urls) {
                //it can be invoked from doInBackground(Void1...) to publish updates 
                //on the UI thread while doInBackground(Void1...) is still running
                publishProgress(Void2...);
            }
            //Runs on the UI thread after publishProgress(Void2...) is invoked
            protected void onProgressUpdate(Void2... progress) {
    
            }
            //Runs on the UI thread after doInBackground(Void1...) has finished
            protected void onPostExecute(Void3) {
    
            }
            //runs in UI threat after cancel(boolean) is invoked and 
            //doInBackground(Void1...) has finished
            protected void onCancelled(Void3) {
    
            }
    }
    

提交回复
热议问题