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.
Related
My Boss told me Not to use AsyncTask<> to hit a webservice. Standard told me use Threads to hit a webservice and then use the Handler to process the response from Thread. Can someone ellobrate how to use thread and handler to hit a API and then give the control back to the Handler , atleast a Tutorial if its possible or Code. What is handler, is there only one Handler in android Application? thnks
Please check it below example:
http://androidexample.com/Thread_With_Handlers_-_Android_Example/index.php?view=article_discription&aid=58
What you are actually trying to do with Thread and update your GUI with Handler is same as using AsyncTask. AsyncTask considers most of the aspects which are somehow difficult to manage. Obviously there are some downsides to use AsyncTask for more details have a look at my answer Down sides of AsyncTask
When to use?
If you're supposed to perform a short time request, then AsyncTask is perfect. However, if you need to get some data and display it but you don't want to worry about whether to download again if the screen is rotated and so on, you should consider using an AsyncTaskLoader.
If you need to download some big data, then you can consider using an IntentService.
I am posting images using JSON. Whenever I take a picture i have to call the webservice.
This webservice call may take some time. I dont want to use Thread nor Asynchronous Task for this.
I want to call the Webservice in background and able to perform operations in foreground(button click, entering some data in edittext etc).
I guess this can be achieved by using Service. Please provide me some tutorials for this.
Is there any way?
Thanks in advance.
Services are basically used when we want to run some code in background even when a application or the present activity gets closed. Use Services only if you need it and be sure to destroy it when you are done otherwise you will waste memory and that is not good.
Here are some great tutorials of implementing service. Firstly make some sample codes to learn how to handle services and see their lifecycle, etc. and then try to implement them in you code otherwise you may be confused xamarin.com vogella.com and technotopia.com. Happy Coding!!!
If you need to handle upload within a serial queue service, take a look at IntentService.
and implement the abstracted onHandleIntent() method.
I want to better understand how to structure an Android app where an activity fires off an API call (for example).
I'd currently implement it by putting the API call into an AsyncTask subclass, passing it a reference to the activity so it can update the UI in onPostExecute. But my gut-feel is that this is creating overly-coupled code.
I'm wondering whether instead I should put an API call like that into a service, and use a BroadcastReceiver to update the activity.
What say you, AsyncTask, or BroadcastReceiver?
I usually follow the Local Service pattern. I have a strong suspicion that this is how the official Twitter app works and that this is the pattern most of the Google apps use. This also solves the issue of your app going away (getting killed or going into the background) before the task finishes, or if the phone switches configuration during a background task.
BroadcastReceiver and service is an overhead here. A request to web-service should not go to long. Service is appropriate in case of downloading files or something similar.
AsyncTask way is the right one here. But I would suggest you showing a progress dialog to let user know that your application isn't freezed, but doing some useful work.
See the example here.
AsyncTask is just fine. Only thing you should worry about is referencing you Activity using WeakReference to avoid whole Activity be memory leaked. It isn't overly-coupled code imo if you using observer or events patterns.
I would go with a service only if the call is going to take long, so that the user can leave the app while it's completing.
I'd use the AsyncTask if the task is short enough that it almost wouldn't go ANR if done in UI thread.
(disclaimer: I consider myself a beginner, and I'm expecting comments from more experienced people)
In my app I need to download files from url locations. I want to display progress of the download in a dialogbox (or optionally in the notification area). I've come across several good resources on this subject (something like http://progrnotes.blogspot.com/2010/09/c-android.html). Unfortunately, all the examples don't provide a clear indication on how to properly cancel a download per the user's request. So my question is actually quite simple:
Given an asynctask which downloads the file in the background (with httpclient) and displays a dialogbox with download progress and a cancel button, how do I can cancel the download and stop the background task when the button is pressed?
I know killing threads is generally not a good idea, so I will probably need to work with a 'cancel'-variable in my background thread. How do I communicate a stop signal from the button to the asynctask?
Regards,
Ivo
Have your button call AsyncTask.cancel(true) and then check isCancelled() from inside doInBackground(Params... params). In this manner, you can communicate to the background thread that the download should be cancelled, and you can take the appropriate steps to stop it.
I would call cancel(true) on your AsyncTask object. This will interrupt your thread via normal interruption handling. You then can ask the AsyncTask if it isCancelled().
I would suggest you to go through this link for the dark-side of AsyncTasks: http://bon-app-etit.blogspot.in/2013/04/the-dark-side-of-asynctask.html .
Google has released a library called "Volley" that is used nowadays for faster and better networking .
It solves Bad points of AsyncTasks.
Canceling request using volley
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.