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!
Related
I want to control a RaspberryPi via android app and plan to do this by defining an api on the raspberry and accessing it via the app.
On the Android Developer Guides I found the recommendation to implement a DownloadCallback interface.
My question is, why would I want to do that, if I can alternatively simply use an AsyncTask and make an HttpRequest?
Using an AsyncTask for network calls is considered a bad idea. First, AsyncTask does not handle orientation correctly so you need to have more code put in so that the asynctask properly terminates (making sure it doesn't make calls to the finishing activity, making sure it doesn't hold any strong references to the finishing activity). You also cannot halt AsyncTask so lets say in your main UI thread, you want to stop the AsyncTask from performing the network call but you already started it. Well you are out of luck and you need to wait until it terminates on its own. (As mentioned in the comment below, this is not true. You can halt interrupt the thread of the AsyncTask to "cancel") Also, you cannot make parallel network requests in an AsyncTask easily.
Also this is more of a preference but using AsyncTask is more boilerplate code than I would like. Retrofit is my most preferred way of network operations.
DownloadCallback isn't about replacing the AsyncTask. It is just a way to communicate between your fragment (the one who started the network call) and the activity (the one who wants to know what the progress of the network call is) about the progress of your network operations. You can see here https://developer.android.com/training/basics/network-ops/connecting that they are still using AsyncTask for network operations but they are using the DownloadCallback to notify the activity about the progress of your operation.
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.
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 !
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
Here's scenario:
Client makes remote call to the service (returns void) and provides
a callback object
Service executes some long running logic on the background thread
and then uses callback object to trigger ether success or failure
which (since these manipulate visual elements) execute in
Activity#runOnUiThread block
The scenario runs fine. The question is - can I use AsyncTask to make
code less verbose (how?) and would be there any advantages in doing it
that way?
Or should I just get away from client callbacks alltogether and
execute remote service calls retrofitted to return some value within
AsyncTask#doInBackground?
It is difficult to say whether AsyncTask will make things less verbose, since we don't know the verbosity of your current implementation.
For me, AsyncTask means I don't have to worry about cleaning up threads myself (e.g., post some sort of kill job to a LinkedBlockingQueue my background thread is waiting on). It also eliminates the custom Job classes I used to create for using with LinkedBlockingQueues. And, it simplifies a bit doing final work back on the UI thread.
In your case, with a remote service, the UI thread issue is less critical, since the activity needs to handle that itself.
I don't see what the difference is between your #2 and your last paragraph. In both cases, your service will call the callback object, which will use something like runOnUiThread() to arrange for the work to be done on the UI thread.
AFAIK, the only two ways to have a service doing any sort of asynchronous work let the client know that work is done is by a broadcast Intent or a callback object. Broadcast Intents are convenient but public (i.e., other code can watch for them).
I suspect I probably have not helped much here, but I just don't know enough of your scenario to provide greater detail.
I'm having quite the same question : i'm developping a map activity, with a 'lazy-loading' functionnality (xml from Network, parsing it, then updating my map with the 'items' created from that parsing...)
i wondered what would be 'the best' way to implement it...
async service launched from a thread, an update notification via Intent?
just a thread (no service, since i don't need to expose it to other applications) w/ callback
asyncTask with callback
i'm comparingthese in terms of speed, using the Android SDK performance analysis Tool traceview
I guess a more precise answer might be found from Android contributors on the Android-developper-group...