-1

I am new to android, i have built an application where lot of web services are used. i am using async task doInBackground() method to make a call to webservices.

my asyntask class is public and it is written in a separate file. i am not using onPostExecute to carry out postwebservice operations, instead i am creating a string and returning back. then in my activity class i am examining the string and packing the data using a java bean.

since Async task runs in an asynchronus way is it correct to rely on what doInBackground method returns. or is it must to perform my post webservice operations in onPostExecute only.

please guide me with the correct way of doing things for my project. Thanks.

user3136798
  • 39
  • 1
  • 4
  • You have to use onPostExecute. – A.S. Jun 05 '14 at 09:38
  • and execute! Never call doInBackground directly.... – ElDuderino Jun 05 '14 at 09:43
  • iam using execute i am not calling doInBackground directly, after calling execute i am getting the response in the activity – user3136798 Jun 05 '14 at 09:59
  • @user3136798 : u have to call `execute` for run an `AsyncTask` after that `onPreExecute()` then `doInBackground()` then `onPostExecute` are called one by one – Kaushik Jun 05 '14 at 10:02
  • @kaushik i know what you said, what i am doing is i am calling execute and getting back the response. but my question is if this approach okay or shall i do my postwebservice operations in onPostExecute only. AsyncTask response = clws.execute(username, password, serverURL); try { stringResponse = response.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } – user3136798 Jun 05 '14 at 10:36
  • You have to use `onPostExecute` otherwise u can't do that see those answers – Kaushik Jun 05 '14 at 11:08

2 Answers2

0

You should do all the post webservice work in onPostExecute, because this method is executed in the main thread (UI) an therefore you can update your UI.

Take a look at http://developer.android.com/reference/android/os/AsyncTask.html

A basic example (Taken from the docs)

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     // Here you call your webservice and return the result
     return totalSize;
 }

 protected void onPostExecute(Long result) {
     // The result from doinbackground is passed as an argument
     // here, so you update your UI from this method.
 }
 }

As kaushik You can also reuse the Asyntask with a defined callback, check this questions:

Community
  • 1
  • 1
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
0

Create an Interface and callback method for accessing AsyncTask class.

  1. accessActivity or Context to ur AsyncTask using Constructor of that AsyncTask
  2. In onPostExecute send the result to ur Activity using the callback method

Tutorials

1.http://samir-mangroliya.blogspot.in/p/android-seperate-asynctask-class.html 2.http://www.androidsnippets.com/asyntask-in-android

3.http://www.brighthub.com/mobile/google-android/articles/82805.aspx

Kaushik
  • 6,150
  • 5
  • 39
  • 54