i want to keep my app in sync with the Server. The communication between client (android app) and server is handled through JSON Objects / HTTP. What is the best strategy if the connection is not available anymore? It is important that the user can continue his work with the app. Does there even exist frameworks for such sync-problems?
i thought about a queue of transactions?! Any reccomendations or experiences?
Thanks
For fetching... I once wrote a caching URL manager that would load read the JSON from reply from the server and write it to the SD Card. When if the user did another request for the same URL a short time later, the URL manager would simply return the cached json from the filesystem as the JSON reply. This made the communication code somewhat transparent, in that I was always dealing with JSON replies, whether or not they were cached or real time.
For sending information to the server, you could write all information to the database, and use a background service that pushes the data to the server. That way the UI will always succeed in writing the information and your sync service would push data if there was a network connection. Using a Service you can simply pass the data in the Service intent and it can worry about the writing to db and syncing, etc.
http://developer.android.com/training/cloudsync/aesync.html
i think this is what i will use in my next project :)
Related
I am using kinvey as a backend to store some data. Everytime the user answers some questions on the phone(Android), the data is sent to kinvey immediately. But there can be a scenario where the user is not connected to the internet, so sending the data has failed.
I would like to know if there is any standard way to storing the data that hasnt been send in an efficient manner? Also how often should I try to resend the data that has not been sent? I would typically just put it in a db, and try to send whatever is in that table every 30 minutes or so and clear the table if the send is successful?
Would this be ideal? Also I see that Kinvey doesnt have a mechanism to handle this automatically(correct me if I'm wrong).
Thank you for any suggestions.
Local database is a good place to store your data securely. The thing is that you are sending data after every 30 min, you have to change that scenario.
Below are the steps:
Check Internet connectivity, if available than work online no need to store data locally
If internet is not available than store it in local DB
You are able to get Internet connection broadcast from system whether it is connected or not if you get connection at that time sync your data with the server (No need to check every 30 min) (System always broadcast when it get connection)
You can took a look at Firebase, feature that you're looking for is described in Offline Capabilities. If migrating from Kinvey is not an option - then store your data inside a DB and use JobScheduler to sync it to the backend when the conditions are right.
The other answers cover most of the use cases.
But if you don't want to use system broadcasts, another alternative would be for you to store the data on a database (SQLite, Firebase, etc), and try some sort of exponential backoff strategy to send your data to the server, using the AlarmManager for example. (Tutorial here).
You can try to check the internet connection from time to time, and send the data. If it fails for some reason (slow connection, connection drops, or no connection at all), try again later. Once you succeed, you can either remove the data from your data storage, or set up a flag like isSent = true.
Kinvey is rolling out offline sync capabilities to all their SDK's, it's currently in Beta. If you use iOS or Javascript for mobile development, check out the "3.0 beta" SDK's. You mentioned you use Android, support for that platform will come soon as well.
In your case, you need a cache store, it will save the data locally and sync automatically if there is no network. You don't have to worry about when or how often to sync.
http://devcenter.kinvey.com/ios-v3.0/guides/datastore#CacheStore
I wanted a background service that listens for data from XAMPP server. I don't want the app to make HTTP requests to check data in the server periodically, instead i want to have an event in the app that gets invoked along the data from the server. This question may seem vague but can anyone suggest me from where to start?? I will be very thankful.
You can use Node.js with express and socket.io, its easy to install and for usage. But also you can use online socket services. And for MySQL querys you can use Sequelize.
I have an Android app that is storing data locally, and a server is set up so that I can backup this data remotely.
My question is what is the best way to ensure that when data is added locally, it also gets through to the server?
I am currently using HttpUrlConnection to POST my JSON encoded data to a RESTful API. But what happens if the server is unavailable (because the Android phone is out of range, for example)? My basic connect timeout is 10s, but if it is out of range for longer than this then the data won't get sent to the server.
Is there a good way to queue my data submissions so that even if I'm out of range for a long while, they will all get submitted properly once the connection is re-established?
A general approach is the following:
Decouple your flow of saving data locally and sending it to server. Keep the two actions separate.
Use a Service to periodically send data from your local Android store to the Server. Plan to catch any errors and build in retry logic. The Service code could determine if the connection is there or not and do the sync accordingly.
Provide a manual action in your app (depends on your design) to begin syncing data to the server or not. Again plan for catching errors and retry logic.
Provide a settings page that the user (if required) can tweak how frequent he wants the sync between the local and server to happen.
I want to send image and text data from Android phone to a Server. i am new plz suggest me the best and easy way to do this task. Server is running a java web service and i will be getting the data from server and also sending the data to server. Thanks
As your server is already there, you will have to use protocol it can uderstand - also SOAP, REST or whatever it uses. So no choice for you.
If you are deigning client-server interacton with android application, you may consider network socket communuication which has less overhead as webservices.
I have an Android App which uses http communication for nearly every operation. I want to be able to demo without connection to the internet by somehow replaying the http exchange. How can this be done? So I want to somehow almost like mock objects but really mock http session so I can always demo the app on or offline. This is really a very cool thing to be able to do. Since you can demo the app easily and reliably. Does anyone know how I could do this. Replicating the whole server side is just not an options its got too much stuff. Its important not to just show screencast but the real data exchange. I just want to be able to run thru the app and replay. Maybe debug as well. Thanks
Here's a hybrid solution using similar ideas from other answers:
You could write a dead simple HTTP server that listens on "localhost:80" (or whatever the port is on the server you're targeting) and point your application to this host instead by factoring out the host name from requests. Your local server has a reference to the actual remote server and does the following:
If ONLINE, forwards the request as-is to the real server, gets the response, saves it locally either in an in-memory cache keyed by the request URL or a file named with the URL as its identifier (munged appropriately)
If OFFLINE, looks up a request in its (in-memory or file system) cache and returns the contents from the cache instead
This is kind of like the record/playback mode that #nicholas.hauschild says.
Now you can just run your app once when ONLINE, causing your localhost server to save away requests that it issues against the real server. Then when you run your app OFFLINE, it just returns these cached contents instead whenever the same URLs are issued.
Hope this helps.
If you're device is rooted, you can use tcpdump as explained in this post: http://www.vbsteven.be/blog/android-debugging-inspectin-network-traffic-with-tcpdump/
or use AndroShark (get if from xda-developers here: http://forum.xda-developers.com/showthread.php?t=725692)
or this one (wifi only): http://www.9bitlabs.com/default.aspx
I would create a "Record Mode", and a "Playback Mode" for my app.
While in Record Mode, I would write out a file each time an http request was made. The file would be named by the endpoint the request is made. The contents of the file would a collection of serialized http requests/responses broken up by line. You could then deserialize lines from this file until you find the proper request, and play back the deserialized response.
This approach would also allow you to create Record/Playback profiles, where you could record multiple different sessions (by placing the files into a different directory) and then playback from whichever profile you choose.
This whole approach could be done with a small wrapper class around the HttpClient object you are using.
One way would be to use an HTTP proxy. Redirect all web traffic to the proxy, which can be running locally on the phone. This could be done with little or no source code change.
find a way using fiddler on pc,and android app take fiddler as proxy.So the http traffic is record.
http://blog.csdn.net/grhunter/article/details/5830199
Simples solution lies in faking it when there is no connection. If there is a error in connection, make sure ur app throws some preset data rather than an error in connection thing.