Android: JSON or XML, caching - android

I'm working on a little Android project in java, I'm using the webapi of spotify to get me so data to play around with.
First thing I notice when visiting: https://developer.spotify.com/technologies/web-api/ is that a developer can choose to use xml or json.
What i would like to do is 'cache' my data so i can do the following:
read one value from the file, or construct an object based on different values
add the object created above to the lisviewadapter
update the listview with the updated adapter
iterate over previous steps until parsing of file is complete
I want to be as quick as possible for my users so that the ui gets updated while parsing and loading the data. For this I sketched 2 use-cases.
add items to the listview until done
show a X amount of items and show a load more button
What file type is more ideal to use for my problem? xml parsing or json parsing?
How can i do this parsing? which tools meet my requirements?
Which use-case seems better to use?
any tips if you have done something like this in the past?
any guidance is appreciated

I don't make any distinction between XML or JSON. To cache data you have two options. Either write the data to a file (will require reparsing of the file to extract the data (unless you create a new file of, for example, CSV data)) or write the required data to a database after parsing. As for updating ListView, that would be up to you based on the number of entries you have.
Android has both XML and JSON parsers built-in. You don't need GSON.

I'd recommend JSON because JSON uses less bandwitch for transfering and google has made an awesome json parser library called GSON.

Related

Displaying raw JSON in a user friendly way

I am trying to create a raw JSON viewer for JSON strings. I understand there are apps that already do this but I am looking to load JSON from my server and display it for an internal tool for my company.
I am able to pretty print the JSON values, but this can still be difficult to read. Are there any Android libraries available for displaying color-coded JSON or JSON that can have its items expanded? If not can anyone suggest a way to do so? I've looked into ExpandableListView but this only goes up to two levels deep
Recommend you to see GSON, it simplify JSON.

what is the purpose of using JSON in android?

I need to know the purpose of using JSON in android ?
Please anyone tell me in a simple way...
Thanks
The same reason you'd use it on any platform. JSON is a way of storing and expressing information. It uses attribute-value pairs in a hierarchical structure. In Android specifically, you may need to download some information from a database, which could be stored in JSON and then read by your app. Alternatively, you could store data locally in JSON but there are probably better and more efficient ways to do that if you're not sending data across a network.
https://en.wikipedia.org/wiki/JSON
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server
For example, you can get data Json if you work with database. Or if you work with some API's then you can get data in format Json.
For example an app could fetch data from a server. When using JSON to get the data, the traffic is quite small and the app can easily work with it.
For example you have a server with a database with recipes, and your app displays recipes, the app could ask the server for recipes, and it gets a JSON in return. for example:
{
name: 'Cookies'
ingredients: { 'Butter', 'Eggs', ... /* I don't know, I'm not a chef :D */
...
}
The app can then just read the properties and display them in a neat list ;)
JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. It is also a subset of JavaScript's Object Notation (the way objects are built in JavaScript
Pls go through this link: http://www.copterlabs.com/blog/json-what-it-is-how-it-works-how-to-use-it/
JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax for describing data objects, but JSON is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages.
Using JSON in Android is not different than using it on any other platform. The main advantage of the format (in comparison to XML for example) is the small size of the data. This is very important for mobile devices due to the scarce resource those application use - i.e. your mobile app should be able to run with little memory usage, slow internet connection and so on.
Besides Android's framework has built-in tools for parsing / creating JSON objects. Thus it is both easy and efficient to use JSON rather than XML. If you have any project specific reason to prefer another data presentation format - don't worry. It is perfectly fine NOT to use JSON as long as some other format is more suitable for your project.
In short JSON is usually the right choice due to its small footprint and easy of use.

Handling large XML files in Android

I'm new to Android development and want to create a Google map app. I have to use 2 large XML files with geo data (latlons), 1 is 2mb and the other 8 mb big.
What is the fastest way to access and parse this data? Is it maybe beter to store this into a database? A webservice is not a good idea I think, because everytime the app will start I have to load 10 mb... And that will process very slow I guess...
Has anybody some good advice?
Thanks!
Keep the whole data in the server and access it from device using webservices. Better to go for SAX parser as the data is too large. If possible better to implement search functionality from server end or to implement pagination from server end.
Maybe you can create some kind of meta data to go along with the large xml file. This meta data can point to specific parts of the large xml file. And each small part can be a dom object instead of a sax parser.
I suggest using sax, creating a summary of the data you need in some kind of hashtable (simple key value pairs) and using it. May be using SharedPreferences for data that does not change.
Also you need to think on how / what data will be used and how frequently. May be you can keep a producer consumer architecture, where a thread prepares the data for use, and the consumer uses it when its ready on a notify or something.
You need to think from 3 angles
Not too much serial processing
Not redoing what you have already parsed
Parsing using the right quantum of data. Infact think of dom for small parts to make your design easier to start with.
And I dont recommend DOM for the entire tree. You need to use a combination of SAX and DOM.
First get it working, and then record response/performance delays and work your way up to improve it from the worst to best.

How to parse strange json

I have a project which requires the json objects to be saved in database and then based on the users action will display info.
I can get the json from the API using here:http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ but need help with parsing the following which is a part of json objects.
It really depends on your data structure and amount of entries. If amount is small and queries / sorting requirements are simple, you may just bypass database entirely and parse JSON with buil it (not so good solution in terman of memory consumption) or pull parser ( like GSON - small external dependency bu way better perfromance )
Another option would be just store JSON documents in database, and some fields from them as separate columns for querying and indexing.
If you like to have complex object hierarchy you will find that you are reimplementing
some ORM solution.

Is there an easy to use XML parser for remote database connection?

I am making an Android app, and I need to connect to a remote database with a webservice.
I will receive an XML file from the webservice with the results of my SELECT query (various rows in some cases).
I don't know much about XML or web services, I only know that I will receive an XML file and that I have to parse it to obtain the data.
Is there an XML parser for this purpose that is easy to add to my app and configure?
I would suggest not using XML and instead use JSON. JSON is much cleaner and much less in kilobytes. Then I would use Jackson to deserialize the JSON string to an object. You really don't need to do any work. Just point to the URL and you get an object back. You just have to make sure on the webservice you also generate correct JSON, which you can also use Jackson.
Here is how easy it is:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class); // can use File, URL, String!
Designing XML parsers are not very difficult. You can probably google for xml parsers and then change them to look for the tags you need or values.
Google is your friend.
You don't need an in depth knowledge about XML but you should know a bit about it. A recommended read would be this article. You should also at least know how the server is handling the communication. Is it a unidirectional communication (your application only fetching data) or is it a bidirectional communication (you are sending requests to the server). If the later how is the server handling them and so on. A bit of background knowledge is required.
I always recommend using the SAX parser as it seems so be the most efficient one due to its concept (being event based). A good read about dealing with XML files on Android can be found here. And also don't forget to search for similar questions here on StackOverflow as it is quite a popular question with some quality answers.

Categories

Resources