Periodical requests with Robospice - android

What is the best way to implement periodic requests in Activity using robospice (ping, for example)?

Someone had the idea of using the retry mechanism of RoboSpice for that matter (see Robospice retry policy custom implementation).
I have successfully used and would suggest using Android Alarms (more details) with a Service that contains a SpiceManager.

Related

Polling periodically a REST API

What should I use if I need to periodically poll a REST api to see if the data in my listView has updated from the server? I though about using a ScheduledThreadPoolExecutor inside a IntentService but It might be overkill? It has to be done in background and if the task is killed when the Activity that contains the listview is destroy, that'd be great too.
What is the modern way to do this now in 2016?
You can write a SyncAdapter by extending AbstractThreadedSyncAdapter. This is well documented here:
http://developer.android.com/training/sync-adapters/creating-sync-adapter.html
Instead of polling the REST API you can use a BroadcastReceiver to receive messages from the server when the data changes. GCM provides the components you need to make it work. See here: http://developer.android.com/training/sync-adapters/running-sync-adapter.html

Android Volley and IntentService / AsyncTaskLoader

I want to refactor my app using Volley as HTTP library.
The current architecture includes using IntentService and AsyncTaskLoader for handling REST requests, and both of them start a new background thread.
Is it possibile to keep the current architecture and use Volley in conjunction with IntentService and AsyncTaskLoader?
I presume that to do this I should use Volley starting synchronous requests inside IntentService or AsyncTaskLoader, but looking at documentation and examples this doesn't look like the standard way to use Volley and maybe it would also decrease the performance benefits of the library itself.
Does introducing Volley mean getting rid of Services in our Android apps architecture?

Performing multiple Network Operations in a Service

quick question, Whats the best way to perform multiple network operations on a service. Consider something like a weather service where one has to update periodically. for each time, i call in this order
getCurrentWeather();
getForecast();
getForecasthours()
which makes a Http request and obtains data in JSON. This returns a new JSON Object for each method which is used to update the UI. Now sometimes, we are not sure how long one of this operations might take, so is using multiple AsyncTasks in a Service a better way to go about this? or is this sufficient enough or are there other better ways to do this. Many Thanks
Async task is a good way. But there are more frameworks that have more features for making json http requests.
Have a look at Google Volley framework.
https://github.com/mcxiaoke/android-volley

Volley or ASyncTaskLoader

They tell everywhere that we should use ASyncTaskLoaders because they are very good at not blocking the UI thread. And there is Volley to use now.
I guess we cannot use both of them because Volley does backgrounding on its own. What do you think? Which one is better?
These 2 technologies are different and hardly comparable. They have different purposes and can also work together. You could for exemple implement a Loader that uses Volley to load the data and inherits directly from Loader (not AsyncTaskLoader, because Volley handles the threading as well).
Main advantages of using Loaders:
Lifecycle is synchronized with the Activity/Fragment lifecycle automatically
Data and loading state is not lost on configuration change
The loader monitors changes and pushes new results automatically to the client fragment/activity.
Main advantages of using Volley:
High performance network stack
Automatic disk cache that respects the HTTP server policy
Powerful cancelation mechanism.
You can combine both to get both sets of advantages or you can use Volley without loaders with its simple API.
I've been using Volley for a month now and I have to say that I'm very satisfied. It really does help a lot not to have to worry about threading implementation details. So far both general networking and remote image loading have been working great.
It's not that there are no issues, but so far they have been minimal.
You better ask like this volley vs Async vs RxJava
You can use this RXJava for background thread, but for better efficiency in calling restful services Volley is highly recommended, also very less coding required compare to async task loaders !
Here is a writeup on current Android best practices. It discusses the use of Volley and RXJava: https://github.com/futurice/android-best-practices
You can combine both, to get both advantages.
In your activity (main thread) you call your API with Volley.
With a simple interface mechanism, you callback your main thread when the data is available.
Then you forceLoad() your AsyncTaskLoader with the fresh data from Volley.
In your AsyncTaskLoader you hydrate all your activity's container. They will be automatically loaded when data is available.
With this approch you combine Automatic disk cache of Volley, and Automatic synchronization of Loader.

Implementing asynchronous http requests on Android with AsyncTask

I'm building this client for a web service.
Pretty much everything makes requests to a server and now what I do is, I open a new thread and put all my requests in the same thread. That means I'm making all my requests in a serial way inside the thread and that turns into a lot of waiting for the user. Aiming to make the application faster, I want to make every server request in an asynchronous way.
I have a Networking class that handles all the HTTP requests I need and I'm thinking of making it so that every request starts its own thread.
I'm thinking of using ASyncTask for this but I noticed that with ASyncTask I'd need a class for each of my http requests (a class for GET, POST, PUT, etc). Is that the best way of doing it? is there a more efficient/clean way of doing this? What do you guys suggest.
Seems like a design decision that will depend on exactly what you are up to. There are various ways in Android to execute tasks depending on whether the user is waiting for some data or is being notified later on once the background task completes.
I would suggest you to look at this post that compares various task mechanisms in Android. Apart from this also go through the java.util.concurrent package.
I'm sorry this is not a concrete answer, but take it from me - it mostly depends on how are you trying to serve the user. So one can only suggest ideas. Hope this helps.

Categories

Resources