asynctask in Android - android

Can anybody tell about the "asynctask" used in android application. Currently I am working on an application where I have to create a Class in which I have to just get the response of any particular URL given.
I this particular class I was told to perform this task by making use of "asynctask". I had been getting very quick responses of all my Questions from here so far and I am greatly obliged to all of them who are helping me since my first question I posted here.
I am quite new to Android Programming and feeling a bit confidence by the community's camouflage with me.
Thanks,
david

Google's documentation for AsyncTask is pretty excellent: http://developer.android.com/reference/android/os/AsyncTask.html
It's basically a construct that makes threading very simple. When you are doing something like making a web request to some URL, you don't want to block the UI thread. However, you usually want to update your UI with the results once your background task has completed.
AsyncTask makes this easy. First, you need to create a class that extends AsyncTask. When you execute the task, its doInBackground method will be called on a background thread. This is where you can download something from the Web or do whatever else you need to do.
The return value from doInBackgroundwill be provided to the onPostExecute method, which can update the UI appropriately.
Again, I recommend checking out Google's documentation. They've got great examples on how to use this.

Related

urlConnection.connect() works only in AsyncTask - android

I am connecting to a web service using
urlConnection.connect();
This works only in AsyncTask. I am not able to execute the above statement on plain activity (without AsyncTask )
Is it the behavior by design, or I am missing something.
By the way, this is my first question
Welcome to stackoverflow. please use google search and stackoverflow search before posting a question. the chnaces are big, that someone else had already asked your question. stackoverflow rules
Network communications must be done in seprate Thread in android.
Read more about it here developer wiki
To avoid creating an unresponsive UI, don't perform network operations on the UI thread.
You are possibly getting:
NetworkOnMainThreadException
because you are trying to perform network operation on Main UI Thread and thus overwhelming it. Instead use an AsyncTask.
Also, note that if the server is taking much time to respond, the main thread becomes unresponsive too.
Refer Android Doc for Developers for more information on this.

Html download with Async Task Android

I'm trying to make a web app using the WebView component. I need to modify the HTML before I show it to the user, so I tried to download it, modify it and the load it. I did this using the HTTP response and get classes and because of a series of exception I had to put them in an AsyncTask. Now the problem is that this solution works but it has a short delay because I have to wait for the Thread to end and then I can call the WebView.load() method either for the home and the other links. It is really ugly...do you have any solution to suggest me? Something without Async Tasks maybe?
The Android documentation states, that you should NEVER perform network operations on the main thread (otherwise, it will give you an exception).
Waiting for the AsyncTask to complete its background workflow is a natural process similar to think over the problem before giving the solution (your brain need to compute it in the background, if you will).
You will wait in any case. The server can't provide information instantly. But if you make request not in AsyncTask, it will block your application. And then android will offer to stop it. You dont need it. Instead of this you need to show something like process dialog.

Synchronous service calls in Android

I'm working on application whose main responsibility is to contact remote server and display the data provided.
Service is Soap based. For that I use ksoap library, but let's cut to the case.
I've been "calling service" with the use of asynchronous tasks.
Everything seemed to go well, but...
Service is sequential, and tends to 'lose' my requests, so I don't get proper results.
So I decided to take a synchronous approach to resolve the issue, but this way I have to provide additional loading buttons/bars etc.
The performance is terrible in this way. What is the best way to handle such case ?
What kind of synchronisation can I use so there won't be any race between the requests ?
How can I make use of Android Services ?
How are those better?
Thank you in advance for answers.
You can actually call the AsyncTask in a sync way:
class MyTask extends AsyncTask<Void,Void,String>
{...}
MyTask x = new MyTask();
String result = x.execute().get();
See the docs page for AsyncTask

Using SOAP with Android implementation recommendations

Im trying to connect to a SOAP service using Android and have read in SO as well as other websites about using ksoap2.
Ive also been reading Android in Action, which indicates that any long running code (> 5 seconds) in a UI thread should be done in either a Handler or as a Service. Are there any recommendations on when to use these as opposed to just using it inside the Activity. Many examples in SO as well as on the internet do SOAP processing right inside the Activity (either in the lifecycle methods or on event handlers) but in my case, I know that there are certain SOAP methods that are going to take more than 5 seconds.
Any pointers or recommendations of when to use Android Handler or Services would be very helpful.
If the UI is depending on the response in that same instance of time, then I recommend you to make use of AsyncTask class. AsyncTask are designed and recommended for such long operations. As an assumption that response of the server is strongly related to the UI than you MUST NOT keep the UI frozen while that long operation is in progress.
You can do these long transactions in the doInBackground() method which will run on background thread, must note that in this method YOU CAN'T UPDATE THE UI, and in onPostExecute()/onProgressUpdate() you update the UI with the response from the server.
Read more on AsynTask if you haven't been introduced alread, here:
http://developer.android.com/reference/android/os/AsyncTask.html
Don't forget to mind the Threading Rules
I recommend using AsyncTask instead. Look it up here: http://developer.android.com/reference/android/os/AsyncTask.html
Basically, you can execute your SOAP calls in doInBackgroundmethod, which is executed in a background thread, and you can update your UI in onPostExecute() method, that way you can avoid the dreaded ANR (Android Not Responding) error.
Have a look at this for an example of AsyncTask.
I suggest you refer the Android Developers page for AsyncTask properly before you begin implementation.
All the best
Cheers

What is AsyncCallBack in Android?

Can anyone give me a clear description of what is AsyncCallBack? Does it exist in the latest android version? Because I am directed to AsyncTask when I search for AsyncCallBack.... Are these both same? Does anyone have an example?
Thanks in advance.
There is no class named AsyncCallback in android. I think what you are looking for is AsyncTask which is a way to run a piece of code on another Thread so the UI won't be blocked, and receive it's results on the UI thread. For example, say you want to talk to a server on the internet in response to the user clicking something in the UI, then receive some result from the server, and update the UI. AsyncTask makes doing this very easy compared to doing regular threading code because threading lifecycle and communication back to the UI thread is handled for you. As a bonus there is also support for canceling a background task, but you have to write the code to handle it when cancel is called. It doesn't do it without some work on your side.
I think the terms may be mixed up here, there isn't an AsyncCallback in Android (as far as I know). There is however very widely used AsyncCallback interface in GWT (Google Web Toolkit): http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/rpc/AsyncCallback.html.
In Android you use AsyncTask to easily run background operations asynchronously on a separate Thread from the main UI/app thread: http://developer.android.com/reference/android/os/AsyncTask.html.
Here is a good intro article on AsyncTask: http://developer.android.com/resources/articles/painless-threading.html.

Categories

Resources