1

I have application that sends (multiple text files) to server and progress bar is updated on each file send & get response. It is dismissed once all files has been sent.

I have successfully implement this by calling send function inside a loop and progress bar gets updated. Now I want to AsyncTask to execute the http request for each file and meanwhile progress bar gets updated as well.

How do I define AsyncTask to send specific file and get the response. On every file it sends, progress bar is also gets updated?

Any help would be really appreciated.

Thanks

-Sam

Sam
  • 423
  • 2
  • 9
  • 23
  • What exactly do you want to know? How to pass a parameter to an AsyncTask? Or how to update the ProgressBar from the Asynctask? – fifarunnerr Jan 16 '14 at 19:12
  • I am calling `new PostDataAsyncTask().execute(currentFile)` inside loop of progressbar it throws exception i.e. AsyncTask can't be called in Non UI thread. How do I then updated ProgressBar and where do I create AsyncTask instance? – Sam Jan 16 '14 at 19:19

3 Answers3

1

If you want to update your ProgressBar while still executing in your AsyncTask, you should...

  • Implement the onProgressUpdate(...) method in your AsyncTask.

  • Call publishProgress(...) when you want to update your progress bar during doInBackground(...)

There is an example of this in the AsyncTask documentation.

Yjay
  • 2,717
  • 1
  • 18
  • 11
1

This has been asked and answered in numerous places. The key is using doInBackground() method to perform the network activity and then you can call publishProgress() from there as you need in order to update the dialog. You will also need to override onProgressUpdate() since this is the method called by publishProgress() and will be where you put the code for your dialog.

Community
  • 1
  • 1
anddev84
  • 1,473
  • 12
  • 30
  • My progress bar spawns in a new thread on button click in main UI thread. Where do I then instantiate AsyncTask? and in this case how do I start progressBar? – Sam Jan 17 '14 at 06:59
  • The idea is, you initiate the AsyncTask in order to do the network I/O for sending your files, and once a file completes, you can call `publishProgress()`, optionally with some parameter. Once you do that, it triggers `onProgressUpdate()` and that is where you should update your ProgressBar. Not the way you are describing it by "spawning a new thread when the button is clicked". Please read the documentation and links. – anddev84 Jan 17 '14 at 13:16
0

You have to call the AsyncTask from the main/UI thread. You can't start an AsyncTask from a background thread. Try calling new PostDataAsyncTask().execute(currentFile) from onCreate(). As Yjay mentioned, you can use onProgressUpdate() and publishProgress() for updating the ProgressBar's value.

fifarunnerr
  • 617
  • 4
  • 12