Android: Asynchronous process using AsyncTask for connecting to internet - android

For my Android app I need to call a web page from activity, and wait until a response is received from remote server. So I think I need to use a new thread. Can I use AsynkTask? How can I tell to my activity that it must wait for respose to AsynkTask process?

check asyncTask
And you can use callbacks to inform the activity that the task has finished
check https://stackoverflow.com/a/13947857/1434631

Yes, you can use AsyncTask
AsyncTask has a method called onPostExecute() which lets you know when your background process, in this case, loading data from server, is completed.
Here is a nice tutorial !

Related

Async Task in Application OnCreate

I plan to make a webservice call in Application onCreate using a asyncTask. The data which the webservice will get will be used in activities later. Since it is a asyncTask there is a chance that it may not complete while the activity wants to use the data. I plan to give some visual feedback to show progress of the data download.
Is there any problem with this whole approach?

Another AsyncTask task?

I have the next simple question. I have an AsyncTask that connects my client to a server. For each task the server does I need another one AsyncTask class?
Let's say that I need a task to fill an array from the server, another to return an integer from the server and one to return an object from the server to the client, I need 3 AsyncTask classes, one for each of these tasks?
AsyncTask is good when you don't need to present any information to your user. Something like saving data to the server, that nobody really cares when it finishes.
When you need something to be loaded for the screen, you'd be better off with AsyncTaskLoader class, that provides all the necessary callbacks.
Besides that, I'd create as many AsyncTasks as necessary. They are cheap, they make the work done. Why worry?
Here is what you can do
1) you can call your 1 st async task from class A and add an interface to that asynctask which has one method like onfirstTaskCompleted()
2) call onfirstTaskCompleted() method in onPostExecute of your asyncTask
3) in class A implement onfirstTaskCompleted() and start an new Async task from there.
and repeat the same steps to 2nd and also third async task
You need AsyncTask whenever you want to do an operation asynchronously.
You do the asynchronous part in the doInBackground() of your AsyncTask.
If you can do everything inside a single doInBackground then you need just one AsyncTask.
Such as connect, get data, disconnect.
If you must do 2 separate asynchronous operations then 2 AsyncTasks. Such as connect, get data in one task. Then present them to the user. Then send request to server to delete some data, say, selected the data selected by the user, this you do in another AsyncTask. Or you can use the same asynctask somehow have a different flow of execution in doInBackground for different scenarios.

Is it a bad practice to use AsyncHttpClient inside/with an AsyncTask?

I'm working on an Android app and there I have one Activity called InitActivity, where I send a request to the server in order register the user and update some data after the registration is completed.
During this action I wanted to show a ProgressDialog as a feedback to the user.
I it is possible to use AsyncTask's onPreExecute and onPostExecute to initialize and show ProgressDialog, while the AsyncHttpClient does its work in the doInBackground method but is there any better solution for this?
Thanks for any suggestions!
Yes, you definitely can do what you have just stated. An AsyncTask is used just for this purpose - to do stuff in the background. It is advisable to always try to lessen the load on the main UI thread in Android. Assuming your registration process is not intensive or time consuming, AsyncTask would be the way to go. According to Android's documentation, you should use AsyncTask for short background processes.
Just a point to note here would be that you would be making the call to display your progress dialog box in your publishProgress() method and not in postExecute() which triggers only after the doInBackground() is complete.

Run code in background, then update UI on completion

In my app I need to fetch some json data periodically from the internet, then I want to update the UI of my app based on this data. How can I execute a background task and then update the UI on completion? Here are some problems I ran into:
Using a service/alarm I can't send my json back to the activity
Using an timer/timertask I can't update the UI because only the thread that created the views may change them
Using asynctask was working fine but I cannot run it periodically
I have been implementing a listener in my activity that is triggered when the json has been fetched. This seems like a straight forward thing to do, I'm sure there must be a solution!
Using a service/alarm I can't send my json back to the activity, use binder to create connection between Service and Activity, or broadcast to transfer data to trigger ui update
Although adding a Binder between the Activity and the Service would have working, I ended up following resus's answer provided here. It uses a Handler to post a new task in the future in the method that is triggered by the AsyncTask's completion.
This worked well for me because I had already set up a listener.

Handle slow response in web service calling in android

I have finished to write webservice in android. But if there is no service or connection or network problem, webservice is searching for that. It takes time. How will handle webservice calling in slow response from webservice?
Can any one assist me to handle this issue?
You need to call the webservice from either a service (docs here) or an AsyncTask (docs here). If you're going to be interacting with the webservice for a while, then I would use the former. Otherwise, I would use an AsyncTask. The main thing to remember is make sure that you do NOT make this call on the main thread or your application will most likely block and Android will display an ANR dialog.
Also, note that, if you use a service, you need to start a new thread and launch the service from that thread to help prevent the application from blocking.
There's a tutorial here that might be helpful.
Hope this helps!

Categories

Resources