I need to make a batch of HTTP requests and feed the responses to a ListView one by one. I am using an async task and running a for loop of requests in a doBackgroundProcess method. Is that a correct approach? If not, please guide me to the best practise.
It's not entirely clear what you're trying to do. If you're doing the following:
Collect a set of HTTP requests
Send them.
Get back the results.
Post to an adapter backing a list view
Wait for the user to initiate the next set of HTTP requests
then I suggest you look into an IntentService. If your Activity goes into the background for any reason, AsyncTask will stop, but IntentService continues until it's done all its work.
I'd even suggest you stick your HTTP results in a content provider. It's best to persist data that takes a long time to retrieve. Your users will like you for it! You can also stop when you lose connectivity, and then re-start where you left off, if you have the data already. And if connectivity isn't available at the start, you can show users the most recent results.
Remember that the network isn't always available.
Related
I want to create a Android background service, which creates a notification, if a server has restarted, but I need some ideas how to implement it.
I thought about a http connection, where the background service waits until a message comes in, but I think the connection can not be keep up while a restart. After this there came up a new idea, where the background service pushes a notification, when the connection breaks.
Would this be possible (if yes, what would be the easiest way) or is there a better way to solve this?
Create an asynchronous task that tries to connect to the server in an endless loop. Use an time limit to cancel the process after a given time or return an ok value if the server responds an 200.
I have a requirement in my Android app, that makes an HTTP request to the server to perform a long running task. The architecture on the server is that; the HTTP request returns immediately with a transaction ID. What I need next is to poll the server to check whether the job has completed and once completed show the result to the user.
When the first HTTP task completes I show a UI Fragment which has a progress bar asking the user to wait while the task completes. Ideally, I would like to show the result to the user on the same Fragment once the task has been done. But I would also need to consider situations where the user might press the Home button or move out of the app/screen in some way.
My current solution is:
Poll every 10-15s with some sort of exponential backoff, as long as
the activity is still attached.
The server sends a push notification regarding task completion - but
I'm worried about reliability of this.
Is there a better way to achieve this? If so, what would be a good design pattern for this? Are there any libraries available that handles this scenario?
One feature of my application is to retrieve live data (JSON object) every 2 sec and display it (only while app is in foreground). I am executing an async task for every 2 sec. But this is making the app slow. I have searched for alternative, but i only got C2DM option. I can't use it because of server limitations. Could you please tell me an alternative or effective way for polling?
One option, if you have control of your server, is that you can switch to something like Comet (long-held http requests) to avoid the necessity of ongoing polling requests.
change the execution of the async task from every two sec to after getting the response for previous update you can initiate it in onpostexecute function... this will make your ui faster
also try using gzip so that the data gets transferred faster do not pool async task
your ui is getting slower as an async task is shot up before the previous one ore ones have completed
currently I have an app that creates all sorts of different requests to Facebook and my server, and I was wondering if the best way to do this is implementing a different AsyncTask or using the same AsyncTask for all the different requests.
what do you think?
Here is a use-case for instance:
I send a Facebook connect request, when I get to onComplete, I get the users Information with FQL (has to be Asynchronous) , when the response comes back, the user's image is posted on the main view.
After this, the app sends a different request to the app's background server and gets a response.
I think you must decide it for yourself.
If you have lots of requests beware of the android's limitation of number of AsyncTasks. If you hit that limit your app will crash.
Also notice that if you assign many jobs to a single AsyncTask you could have a very long running task on the background.
You can also read this article -> The Hidden Pitfalls of AsyncTask.
I'd suggest to use concurrent AsyncTasks to ensure all the other requests are run in case one of them won't return an answer.
I have written an application that queries a web service I wrote (which returns JSON data). My app currently processes the web service call using an AsyncTask and updates a TableLayout with the data it receives. I want my app to regularly (every second or so) call this web service and display the data in the DB, as it is continuously being updated. How can I go about doing this without having the UI thread block?
Currently the way things work is the user presses a "go" button, and the AsyncTask displays a "loading" dialog while the request processes. I would like for them to press the go button once and have the data refresh in the layout. I'm not sure what the best way to architect this is.
I wouldn't recommend that you create a new AsyncTask every second since this is going to result in a lot of object creation and corresponding memory collection.
What you can do instead is create an AsyncTask that after each request returns from the web service updates some internal data structures and then calls publishProgress(), waits the appropriate amount of time, then makes a new request to the web service. In onPublishProgress() the code should then get the new information from the request from whatever internal structures are being used (don't forget to use a lock here to synchronize access) and refresh the UI.
You'll also want the AsyncTask to have a method or variable that the Activity can call to tell it to break out of the loop.
You can use a Handler which can initiate a new AsyncTask request after every second.