[Solved] how to show toast in getDataTaskmethod? [duplicate]


Your toast message is within the parseJsonData method which is called from the doInBackground method of your asynctask.

You can not update the user interface thread from a background thread.

You have two options here

1) You can publish the progress publishProgress(1) of the thread passing in an integer value to be used as a flag which you can pick up on in the onPublishProgress listener and show your toast there

or

2) As your method has finished by this point then make the parseJsonData set an integer variable global to the asynctask and in the onPostExecute method pass something back to the listener to indicate that a toast needs to be shown

Update based on comments

Replace

  {
      Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  }

with

  {
      publishProgress(1);
  }

Add the missing onProgressUpdate() method to your asynctask

@Override
protected void onProgressUpdate(Integer... percent) {
  //Call your listeners.onProgressUpdate(percent) here and show the
  //Or
  super.onProgressUpdate(percent);
  if (percent[0] == 1){
     Toast.makeText(thirdstep.this, message, Toast.LENGTH_SHORT).show();
  }
}

I’m not here to write your code for you. Do some research on how to properly write an async task and publish progress

Here is a good starting point
http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

You should be aware of orientation changes and how that will effect your asynctask (I avoid the pitfals of this by using a fragment

This is the design pattern I use for async tasks
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

But for handling web services, be nice to your users and let the android system work out when to download data etc and don’t drain their battery and use a sync adapter with an intentservice instead of an asynctask. There are already too many crappy apps out there that take the asynctask approach for consuming web services. Please don’t add yours to the list

Do it this way
http://developer.android.com/training/sync-adapters/creating-sync-adapter.html

It’s a lot of extra learning curve but your a programmer right? You should be giving your users the best possible experience.

BTW You are getting down votes because you are demanding code to be written for you. I’m hoping this is just a language barrier and not an attitude problem.

6

solved how to show toast in getDataTaskmethod? [duplicate]