Stop HTTP requests in async tasks on changing activity - android

I am downloading multiple images in an activity to populate a list view, all in seperate async tasks. In this activity the user can press a button to go to another page which doesn't require the images.
The issue I run into is that even on going to the next activity the async tasks are still running and the httpclient isn't released for the requests that need to be made in this next activity.
I tried using asynctask.cancel for all the tasks running, but that throws an interruptedIOException.
Is there any other graceful way of stopping the requests that are being made to free up the client?

Ideally, what you want to do in these kinds of situations is to wrap your HTTP requests in a loop that can be cancelled. Using this question as an example (full implementation there):
#Override
protected Void doInBackground(Void... params) {
while (running) {
// loop your HTTP requests here
}
return null;
}
When you trigger the onCancelled() method (by calling cancel), your doInBackground method will terminate as soon as the current HTTP request is complete (and not before).
This is likely to be the most graceful solution you're going to get using AsyncTask. Also, you might have to catch the interrupted exception somewhere in your AsyncTask class, but this should be enough to get you going.

Related

How to update the UI smoothly?

So from what I've read, Android's AsyncTask is a great way to asynchronously load information from the Internet. However, I don't want to block up the UI and prevent the user from interacting with it.
A basic description of my problem.
Currently, I am using websockets in order to send/receive data from a web server. On events like a user entering the room, a song being added or removed from a playlist, a song being upvoted or downvoted, or one song ending and another one beginning, the UI must be updated in order to indicate changes. But ideally, these changes will be occurring very frequently, which means that constantly blocking the UI in order to refresh it would be cumbersome and annoying.
How would I update my UI without interrupting the user in their activities? Would AsyncTask suffice?
The asyncTask does not block the UI. It runs on a separate thread to send / receive the data from the web, and then returns the results. When you receive the results back, you can update the UI as you choose.
Your UI will not be stopped while the asyncTask is performing its background work. You can try it out by by building one in your activity and simply sleeping for some amount of time (let's say five seconds) in the doInBackground method. You will see that your UI is still functional during that five seconds.
Edit: You can do just about anything with the results you get back and it won't interrupt your UI either. If that's not the case, you'll probably want to look at optimizing what you are doing with your in memory objects. Anything not stored in memory should probably be retrieved or written to disk, database, or internet endpoint with an AsyncTask. As the commenter points out above, this is not the only way to use other threads, but it's easy and will probably work if you're making a reasonable web request and expect users to have a decent connection. You will just want to make sure you have timeouts and exceptions covered so that your app doesn't crash if the task takes longer than expected.
public class LoadCommentList extends AsyncTask<Integer, Integer, List<Comment>> {
private String commentSubject;
public LoadCommentList(commentSubject){
this.commentSubject = commentSubject;
}
// Do the long-running work in here
protected List<Comment> doInBackground(Integer... params) {
// the data producer is a class I have to handle web calls
DataProducer dp = DataProducer.getInstance();
// here, the getComments method makes the http call to get comments
List<Comment> comments = dp.getComments(commentSubject);
return comments;
}
// This is called each time you call publishProgress()
protected void onProgressUpdate(Integer... progress) {
// setProgressPercent(progress[0]);
}
// This is called when doInBackground() is finished
protected void onPostExecute(List<Comment> comments) {
// calls a method in the activity to update the ui
updateUI(comments);
}
}
There are cleaner examples actually using the Integer... params for example, but this is just something I had handy as an example.
I don't know where you read that but asyn task are worst way to make web service call this days. You should use Retrofit for service call, it is 8 Times faster and handle UI update smoothly.
Read more about this here :-
http://googleweblight.com/?lite_url=http://instructure.github.io/blog/2013/12/09/volley-vs-retrofit&ei=qR4bQU5c&lc=en-IN&s=1&m=260&host=www.google.co.in&ts=1465531978&sig=APY536z0v15lfX3G6KY4nls4wf1kzttJdA

Show a message when doing synchronous server request

I am working on an app where i do some calls in synchronized manner using the
class HttpUtil extends AsyncTask<Void,Void,String>
{...}
HttpUtil httpUtil = new HttpUtil();
httpUtil.execute((Void[]) null).get();
This will call to a AsyncTask method.
Issue:
The spinning wheel is not shown in the UI since we do a UI blocked request. Even if I add a toast then the toast is displayed after the request is completed.
If I make asynchronous calls then I get the spinning wheel as the UI was not blocked
Expected:
I need to show a spinning wheel for the blocked request(done adding get() method) also.
Do not use get() on a network operation. Just don't. Your app will freeze, and either get killed by the system, or the user will be frustrated that your app blocks the entire UI of the system. It's absolutely unacceptable, and there is no reason why a well-designed app should need to resort to that.
You turn on your indeterminate progress in the AsyncTask's onPreExecute(), and turn it off in onPostExecute(). These two methods are always run on the UI threat. Please refer to the documentation for AsyncTask.
Further, you won't need to pass Void[] null to the execute() call -- just pass nothing, which will result in an empty array of Void.
If any other operations or UI updates depend on the result of the request, then do those updates in onPostExecute. If you want to create modality to essentially "halt" the UI while the request is running, then display a dialog box, but please provide a cancel option.

Android - Two AsyncTask running parallelly - Exception

I develop a mobile app where clicking on button in Activity A triggers Activity B as well an async task. In Activity B, there is a async task which gets trigerred in the oncreate method. So, there will be 2 async task which will be running parallelly. both the async task interact with the server. (POST and GET method respectively)
All is good when the server is up and running. When I make the server deliberately down and click on the button in Activity A, I am not sure where the control is going. I expect a connectiontimeout exception and I am getting it. But sometimes the async task in Activity B's exception occurs first and sometimes the async task in Activity A's exception occurs first.
In the catch block in both the async method, there is an intent which starts anotheractivity (No server Connection activity)
Once after the exception ( in both of the async methods) has occured I am not seeing any logs in the Logcat. I believe the app comes to a standstill. I wish to gracefully inform the other async task which ever is running to stop.
Could anyone please help me in accomplishing this task. Is there any good design approach to handle this? Let me know and thanks for your time and effort.
Consider implementing your own timeout detection and handling.
Use handler and provide timeout period when firing the ASyncTask
private void readFromServer(String url, int timeout) {
// read from server
final ServerHit serverHit = new ServerHit(this);//ServerHit is my asyncTask
serverHit.execute(url);
// set a timeout
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (serverHit.getStatus() == AsyncTask.Status.RUNNING)
{
serverHit.cancel(true);
}
}
}, timeout);
}
However if you call only one instance of each AsyncTask consider declare it as a static or let it follow the Singleton Pattern. And upon cancelling one task just cancel the other one.
EDIT
You can define those asyncatasks as static and can cancel them on timeout
You should read the first "Related" post on the right "Is AsyncTask really conceptually flawed or am I just missing something?".
Your attempt to do what you are doing, shows little understanding of how the Android OS manages Activities and their AsyncTasks. Trying to start one from Activity A and then start Activity B before it finishes is doomed to fail.

Mono Android: terminate thread in onPause() state

I am running into a strange problem...
My application is meant to do some webservice calls on a separate thread. Once the webservice call is finished, it would navigate user to a different activity.
In the case when user press the home button or exit current activity it should terminate the webservice if the webservice call thread is still running. Hence I put a thread termination method in the OnPause state.
Here is the method block that is running inside the thread:
private Thread _webserviceThread;
void WebserviceCallThread(){
WebRestult result= WebserviceCall();
if(!result.containsError()){
RunOnUIThread(delegate{
transitionToActivityXYZ();
});
}
}
void RunThreadAction(){
_webserviceThread = new Thread(new ThreadStart(WebserviceCallThread));
_webserviceThread.Start();
}
protected override void OnPause(){
if(_webserviceThread != null && _webserviceThread.IsAlive){
_webserviceThread.Abort();
}
}
After the webservice call is done and begin the transition to another page, It gets to the OnPause state. However, in some strange cases, it would think that the thread is not finished in the OnPause state, even though the activity transition is the last line of the method.
Has anyone ran into this problem before? If so, how did you solve this problem?
Thanks!
I always use AsyncTask for this kind of thing. Not only does it abstract away the explicit thread handling and provide hooks to do everything you want here; it's also a nice way to represent a unit of work that can be used from other activities.
There's a simple example in this post part way down, but it doesn't use the generic parameters which are quite handy.
Why not use Task Parallel Library,
It is standard .NET, and with AsyncTask, it is only recommended for tasks that take less than few seconds. see the Documentation
AsyncTasks should ideally be used for short operations (a few seconds
at the most.) If you need to keep threads running for long periods of
time, it is highly recommended you use the various APIs provided by
the java.util.concurrent
Below is an example for how to use Task Parallel Library, taken from here
private void loginWithTaskLibrary()
{
_progressDialog.Show();
Task.Factory
.StartNew(() =>
_loginService.Login("greg")
)
.ContinueWith(task =>
RunOnUiThread(() =>
onSuccessfulLogin()
)
);
}

How to cancel Service/IntentService/AsyncTask/Looper

I'm going nuts here.
I want a simple thing - I have a long task (fetching several data objects from the web) and I want the ability to cancel it.
I tried a lot of things (a lot) and nothing works
The flow goes like this:
the user click on a button
I start the work (I tried with AsyncTask, Service, IntentService and Looper)
the task takes care of everything including adding ongoing notification for progress updates
the intent in the notification has a call for a new activity that her only purpose is to cancel the ongoing task
in the cancelActivity I tried to call stopService() for Service/IntentService or do
Looper.quit() for the Looper (I don't remember what I tried for AsyncTask, not sure if there is such api for canceling it)
In my point of view the best option will be using IntentService (I could have several task lining up and IntetService will do it in order like I want) but
I'm open to suggestions for any type of implementation - I don't care what the code will be, just that I will have the option to cancel the task
Thank you in advance for your help
Dror
(I'm off to bed - 8 hours on the same issue is just too much)
It does not matter what specific operation you use to stop the task if you don't recognize the stop condition in your background logic. The only way to cleanly accomplish it is if background worker stops and exits in good faith.
There are few possible scenarios and solutions that you can use for canceling background work.
Background thread executes many short steps (for example computation with some loops). In this case, check some flag (could be isInterrupted()) between operations and exit if this flag indicates that operation must stop.
Background thread is waiting on monitor. Call interrupt() on background thread, catch exception in in exception handler make appropriate steps to finish this task cleanly and exit.
Background thread is waiting on IO. This use case is very hard to solve in general case. If you use some socket, you can try closing this socket externally and catch the exception. In worst case scenario, you can just abandon the thread in the state that if it ever returns from IO it knows that it is canceled and IO results must be discarded. If you do it often - you will run out of memory, so I would not really recommend it.
In any case, there is no way (except killing the thread which is really bad) to stop your task if it does not know about possibility of being stopped.
Ok, i manged to do something close to what I want.
I'm using IntentService that will queue my task. each new task is AsyncTask. the AsyncTask starts with sending notification with pendingIntent for cancelActivity.
When clicking on the notification the user gets a warning popup about stopping the task. If he clicks yes than I do stopService() on my IntentService.
In the IntentService I added:
#Override
public void onDestroy() {
currentTask.cancel(true);
if (mNotificationHelper != null)
mNotificationHelper.completed();
super.onDestroy();
stopSelf();
}
in the AsyncTask I added:
#Override
protected void onCancelled() {
isCanclled = true;
httpClient.getConnectionManager().shutdown();
mNotificationHelper.completed();
if (mAsyncTaskListener != null)
mAsyncTaskListener.AsyncTaskCanceled();
}
so that will drop all the connection currently in motion. In the actual code that do the work I need to catch the exception for connection shutdown and handle it
so in the end I'm not actually stopping the AsyncTask/Service but rather exposing the httpClient so I will be able to drop the connection in asynchrony why.
I think is a bit ugly but I got no other way
thank you all

Categories

Resources