Android sync data to internat storage with Retrofit Service - java

I want to have some data stored in Device in order my application to work offline. They are plain java objects that are returned from a JSON web service. I use retrofit + GSON . The issue is how to store them.
WHAT I have tried
Tried to use Serializable interface at my java classes and during my sync() method when I fetch them from webservice I store them at a file. Performance is a bit poor at slow devices during sync. Not much but it is an issue.
WHAT I attempted
I attemted to store JSON from Response Retrofit. However it seems a bit of pain. Because it doesnt return as string the result but as InputStream. Then I have to convert back again.
WHAT I didnt do
I didnt try SQLite and any ORM. Seems overkill to me for 7 simple list of readonly List of objects. And I guess that performance is going to suffer more.
In IOS core data is easy to achieve this out of the box. Is there a solution for this in android ?

Best approach to this is to write the JSON out to a file using one of the near infinite number of free libraries or code samples, such as: http://www.mkyong.com/java/json-simple-example-read-and-write-json.

Related

Read REST api response hosting very large data

I am calling an REST API endpoint which is hosting a very large amount of data. The data quantity is too much that my chrome tab crashes as well (It displays the data for a short time and it's loading even more data causing the tab to crash). Even postman fails to get the data and instead would only return 200 OK code without displaying any response body.
I'm trying to write a java program to consume the response from the API. Is there a way to consume the response without using a lot of memory?
Please let me know if the question is not clear. Thank you !!
A possibility is to use a JSON streaming parser like Jackson Streaming API (https://github.com/FasterXML/jackson-docs/wiki/JacksonStreamingApi) for example code see https://javarevisited.blogspot.com/2015/03/parsing-large-json-files-using-jackson.html
For JS there is https://github.com/DonutEspresso/big-json
If data is really so large then better to split task:
Download full data via ordinary http client to disk
Make bulk processing , using some streaming approach, similar to SAX parsing for XML:
JAVA - Best approach to parse huge (extra large) JSON file
With such split, you will not deal with possible network errors during processing and will keep data consistency.

How can I save the data my app parses, so that it doesn't need to be parsed when the app is reopened later?

So I am working on my app, and it parses data from the LoL (League of Legends) API, so far so good, but the only problem is that if the user closes the app, and reopens it parses the data again. I want the objects I parsed from the JSON data to be saved, once it initially parses them, instead of redownloading them every time I reopen the app. It slows my app down.
There are a few ways to do that, but they all basically doing the same thing: serialize/de-serialize objects. The difference will be mostly what kind of persistence storage and what serialization mechanism are you using.
As for persistence you can use for example file, data base, etc.
Java provides human-unreadable binary serialization. Instead you can use JSON, XML, etc. formats. I assume Java built-in serialization will be the fastest, but it won't be deserialize if your class changed in the mean time.
Local persistence also would allow to map objects you parsed into database and load them pieces by pieces as needed.
Save your JSON data as a string in Android SharedPreferences. It is the most convenient and fastest method for you.
Using SharedPreferences

How to fetch large JSON response in android Sybase Unwired Platform

I know normal parsing in Android and Java .
But , here the case is bit different .
In cases when the default String (300) is set I can fetch JSON String with PAGESIZE being set as somewhat 2096 and I can generate code in Sybase.
But if I want to get data as large which is more than size 300 characters or if 300000 characters then the MBO hit can get it but not via code in Android .
In android code I get TRUNCATED data + some data of JSON fetched after I hit for synchronize
So I need to know how can I fetch large JSON string as result via Android code
.
Kindly note : This question is for Sybase Android application development
A similar question has been posted in SAP forum but yet to answer
http://scn.sap.com/thread/3738150
for large data try to save your data in a database from the
beginning (it's a better practice), then deal with it using SQLite.
Lets asume when you talk about fetching you mean fetching data from a server. 2 years ago the most robust approach was to use the gson library for android to parse the json string element by element.

WebService - Overview

There's a lot of info out there for a newbie like myself when it comes to webservices, I acknowledge that.
However, most of the Google results I've seen tend to be focusing on a specific format or strategy, and all of them different to each other.
As a newbie, I am looking to get more of an overview of the various options open to me, their pros & cons... before I start committing to a specific one.
For example, I have an existing webservice created from a SQL Server source via ASP.net and this by default comes out as a DiffGram. So there are articles that tell me how to parse the diffgram data coming into my Android app, but I still do not know if the diffgram is the best option I should be using in the first place.
Should I, for example be changing my webservice to output JSON ?
Does anyone know of a 101 level tutorial or explanation out there ?
Many Thanks
DiffGram is just another type of XML format. In order to parse the XML data in Android, simply use the XMLFullParse class.
FYI: http://developer.android.com/training/basics/network-ops/xml.html
However if you just want to create a simple data exchange WebService, Json is definitely a better choice. Json is simple, lightweight, easier-to-parse and surely easier to use.
Example:
JsonObject json = new JsonObject(jsonString);
json.put("name", "Michael");
json.put("age", "18");
String name = json.opt("name");

Integration with Facebook Graph Api - update mechanism without Real-Time Updates

I'm working on a project which uses Facebook Graph Api to download information about users of our application and their Facebook friends. There is a need to update this data, but as I understand Real-Time Update is not an option. For example I would like to have update of profile feed of friends of our app user, and I don't see a way to do this with Real-Time Update.
Could someone give me some advice on this update mechanism? I need to update app users, their friend connections and profile feeds of users and their friends. I understand I'll have to poll Facebook servers to retrieve this data. What I'm trying to find out is some good practices when doing these things. Update frequency? Way to recognize that data has changed? If anyone has experience with this kind of things every advice would mean a lot.
Thanks.
You can use the since= query string parameter of the Graph API call. Here's some pseudocode to help you along
var usersLastPostDate = GetLastPostDateFromDataStore(userId);
if(usersLastPostDate not populated) {
streamItems = GraphApiGet(userId, "me/feed")
lastStreamItemDate = GetNewestStreamItemDate(streamItems)
StoreLastPostDateIntoDataStore(userId, lastStreamItemDate )
}
else {
streamItems = GraphApiGet(userId, "me/feed?since=" + usersLastPostDate )
}
Not massively useful for your use case (as you're wanting to get data which changes frequently), but worth pointing out that the Graph API now supports ETags - https://developers.facebook.com/blog/post/627/.
ETags will tell you if the data has changed since the last time you requested it. This won't stop you from hitting Facebooks API throttling limits, but is a quick and easy way to tell if the data has changed since you last asked for it.
There is no one answer to your question, as it depends on what your application is doing. How often do you need to get the updated information? If your data is stale for 5 minutes, is that really a problem? Can you grab the data from Facebook lazily, when some user action requires that you have it?
If you do need to do a lot of polling try and use non-blocking IO, especially if you're expecting to have a lot of open HTTP requests to Facebook whilst you're polling. Build a reliable queueing mechanism and HTTP poker to ensure requests are being made as expected. Without any idea of what technology stack you're using it's hard to be more specific than that.
HTH
What about this: Open Graph Subscription system ?

Categories

Resources