Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
we can do this in android.any problem ??
if Yes than tell me please.

private class getDataMoneyRequistion extends
AsyncTask<string,> {

@Override
protected String doInBackground(String... params) {
String str = params[0];
if (str.equals("1")) {
//somecode
} else if (str.equals("2")) {
//somecode
}
return str;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result.equals("1")) {

new getDataMoneyRequistion().execute(new String[] { "2" });
}
} else if (result.equals("2")) {

displayDataFromMoneyRequisition();
}
}
Posted

1 solution

Yes, you can do this, but I'd recommend you check the result in onPostExecute and set a flag to true or false (as the coding guidelines recommend), and then check it back in your main thread (from where you called the AsyncTask)

If the result returned is what you expect and the flag is properly set, then instantiate it again from the main thread. It is poor coding practice to call an AsyncTask from another AsyncTask.
 
Share this answer
 
v4
Comments
Rajesh Sanandiya 7-Sep-12 5:50am    
First of Thank you for Answer.
my Problem is:
I call AsnyTask From OnCreate.
And I need to call Sql Server two time at the time of Activity Load(or OnCreate Call).What can i do?? so that improve coding practice.
Andrei Straut 7-Sep-12 6:10am    
First off, there's nothing wrong with calling an AsyncTask from an Activity.onCreate. I do that all the time.

Secondly, if I understood you correctly, you need to call an SQL Server twice from an Activity.onLoad, as well as an AsyncTask.
If the two are related (SQL Server call depends on the AsyncTask or the SQL call is in the AsyncTask), you should wait for it to finish (in the meantime displaying a "Loading" dialog), and in AsyncTask.onPostExecute do what you need to do.

So the flow would be something as follows:

1. Activity.onLoad
3. Show a dialog
2. call AsyncTask.execute
3. AsyncTask is finished - call the second AsyncTask
5. Second AsyncTask is finished - close the dialog
4. Do your thing

As for my recommendation from the previous solution, I'd suggest you make a method (public) in your activity that acts as a wrapper around the AsyncTask.execute call.
Then, in AsyncTask.onPostExecute, call the method in your activity to execute the task the second time
Rajesh Sanandiya 7-Sep-12 6:24am    
sorry for poor english.
I agree your answer,but my need is:
my first AsnycTask.execute,Load Sql Server Driver(jtds lib).
and second AsnycTask.execute,GetDataFrom Sql Server.
my Queation is :
Both AsnycTask call at the time Dailog open?
if Yes.than i think before driver load Query is Execute.
so nullPointer error occure. Because Both AsnycTask Execute Concurent.
i need first Driver Load using FirstAsnycTask Than Query Execute using SecondAsnycTask.
What you think?
Andrei Straut 7-Sep-12 6:28am    
You'd have to come up with a way of opening the dialog from the first AsyncTask and closing it from the second. It would be no point in opening the dialog from the first, closing it, and the doing the same from the second.

Also, as I've said before, you don't actually call both AsyncTasks concurrently, you wait for the first to finish before calling the second. And in this particular case yes, it might be better to call the second from the first's onPostExecute, as you wrote in your original question
Rajesh Sanandiya 7-Sep-12 6:53am    
Thank you...
Second Question:
i use JTDS lib for Connect Sql Server.
No any Problem android 2.2,2.3.
But When i install Application in Hcl me u1.
it give error:java:sql:Exception:BUFFERDir connection property invalid.
After two day I reInstall Application and Completly Run.
What is Problem?
JTDS Not Supported in Android?
i think No,Because android is Base on java so that it is not possible to not supported.
what you think about?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900