AsyncTask and error handling on Android

前端 未结 12 2157
失恋的感觉
失恋的感觉 2020-11-27 10:32

I\'m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the

12条回答
  •  时光取名叫无心
    2020-11-27 11:08

    Personally, I will use this approach. You can just catch the exceptions and print out the stack trace if you need the info.

    make your task in background return a boolean value.

    it's like this:

        @Override
                    protected Boolean doInBackground(String... params) {
                        return readXmlFromWeb(params[0]);
             }
    
            @Override
                    protected void onPostExecute(Boolean result) {
    
                  if(result){
                  // no error
                   }
                  else{
                    // error handling
                   }
    }
    

提交回复
热议问题