Do you advise clients to use JSON or XML? And Why? - android

Many times my client ask me whether they will deliver data via XML feed or JSON strings. I usually say:
XML if you already have a feed and do not have a web developer who will create script for generating JSON strings
JSON if you do not have any feed and need to create any from the scratch
What do you say? Do you think that delivering data via XML feeds is obsolete and that XML is over-complicated and too heavy?
Should I advise all clients (for the sake of the future) to move onto JSON way of delivering data?
EDIT
From another discussion https://stackoverflow.com/questions/2636245/choosing-between-json-and-xml I can see that JSON is advised for web services, which is the most used case scenario in my clients. It seems that I was advising them properly.
What is they want to pass news articles onto a mobile device - shall I advise XML of JSON?
What about post&get cases when I need to post some data and the to get the response which will be displayed on user's mobile device - XML or JSON?

If the consumers are browsers or mobile devices, I would recommend JSON.
Faster
Lighter
Native parsing support
If the consumers are other programs, I would recommend XML
Can be validated easily
Code generators available to make programming easy and is less error-prone

JSON - if you have a choice :) Google GSON is a serious help there.

We Use JSON: If we want to serialize a data structure that’s not too text-heavy and all you want is for the receiver to get the same data structure with minimal effort
We use XML:If we want to provide general-purpose data that the receiver might want to do unforeseen weird and crazy things with, or if you want to be really paranoid and picky about i18n, or if what you’re sending is more like a document than a struct, or if the order of the data matters, or if the data is potentially long-lived.
This discussed topic might help you .

I agree with all the other recommendations for JSON, but for me the main reason for going with JSON is it's far easier to process on the server especially if you are using a language that supports the JSON structure natively (e.g NodeJS or Python).
I would not say XML is obsolete though. The one obvious case where XML wins is readability. As a programmer I would say JSON is just as readable but I've worked with a lot of people (mainly web designer types) who prefer the look and feel of XML, probably because they are already intimately familiar with HTML.

I agree with your assessment really. Json is easier (for a human) to read, more intuitive and lightweight. XML is better if you have lots of existing XML solutions/interfaces that you're plugging in to. I see XML as the established, mature heavyweight of structured documents, but you don't always need an established, mature heavyweight. It all depends on the use case.

Related

Best way to update sqlite on Android

I would like some advice. I'm going to be using an sqlite database that will be pulling down information from my server and then saving it in the DB then displaying it. Could someone advise me of the best way to populate the DB, should I...
Use a http request and return a string de-liminated with say a | and use a loop to write to the data base.
Use a JSON to retrieve the information and then store it in the database.
The information is going to be just text and some fields will contains links to images I want to then download (get to that later). Just wanted some advice on best practices. I have done some searches on SO and other sites but can't find much advice. Also as a side note any examples you know of that are good for noobs :)
Based on what you write here I would pick JSON.
To core points:
JSON is a standard format.
Android ships with a JSON lib (org.json) making it easy to handle it (encode / decode data).
JSON is known by a large community so you can ask questions and get them answered rather easily. With a custom format you cannot tag the question as 'json' here at SO... ;-)
Using standard formats and libraries helps you to avoid designing and implementing this stuff yourself, which makes your software more robust.
Sometime later you might need to add more complex data to your project. By that time it will be rather straightforward to use JSON's array and objects. With your private scheme you will have to add this capability to it and extend your parsing code. That can easily introduce subtle bugs. Or you might decide at that point that it is too hard with your custom format and decide to move over to a standard like JSON, XML, etc. At that time it costs you much more to shift over than if you start with a standard format. Consider time invested to write and test the current code and then the extra time to change to the standard format for the current system.

best method for xml data storage

I am a php/mysql developer learning android. I am creating an android app that receives info from my php app to create list views of different products which will open a web view of that product's detail.
Currently my php cms web application outputs xml lists for an iphone app.... (also, separately outputs html). I have full control of the php app so if there is a better way to output the data for the android app please let me know.
I have created code that reads the xml from the web and creates the list view. The list can be refreshed daily, so the data does not need to be read from the online xml every time the app starts.
So I was thinking to store the data retrieved locally to improve my apps responsiveness. there may be up to 500 product descriptions to be stored at any given time in up to 30 different xml lists. I am starting development with one xml list with about 30 products.
For best performance should i store the product info in a sqlLite db or should i store the actual xml file in the cache/db or some other method like application cache.
I also was think to create the update of the data as a service, would this be a good idea?
The most efficient way to store data is RAM. But if you want to cache it, then the most efficient way is Database.
I recommend you store your data in sqlite android database.
You could also consider zipping you xml for faster network transfer and unzipping through java.util.zip package classes. You could even consider a simpler format for transmitting data, less verbose than xml, using a datainput/outputstream.
(I do that in of my apps and it works great)
Here are some details on data input / output stream method :
imagine a proprietary protocol for your data, only what you need. No tags, no attributes, just raw values in order.
on the client side, get an input stream on your data using URL.getContent() and cast it in input stream.
on the client side still, build a data input stream encapsulating your socket input stream and read data in order. Use readInt, readDouble, readUTF, and so on.
on the client side, from php, you need to find a way to save your data in a format that is compatible with the data format expected by the client. I can't tell much about PHP, I only program using java.
The advantage of this technique is that you save bandwith as there is only data and no verbose decoration due to xml. You should read about java specs to understand how double, int, strings are written in data output stream. But it can be hard using two languages to get the data right.
If php can't save format in a suitable way, use xml, it will be much simpler. First try with just plain xml, then give a try using a zip or tarball or xml file.
But all this is about speed gain during network connection.
The second part of what you have to do is to store each row of your list in a SQL table. Then you can retrieve it pretty fast using a CursorAdapter for your list view (it breaks the charming MVC model but it is quite fast !).
Sorry about this, but it became too long to write as a comment. This is not intended to be an answer to your question, because in my opinion Stéphane answered very well. The best solution is indeed to store the data in an sqlite database. Then you need to create the class to be used as a connection between the data, the database and the app. I don't want to take credit for what is said here already (I, too, voted it up).
I'm concerned with the other suggestion (use of low level raw streams for data manipulation, the list steps on that answer). I strongly recommend you to avoid creating your own proprietary protocol. It goes like this:
I need to exchange data.
I don't want to deal with the hassle of integrating external APIs into my code.
I know I can write two 5 minute routines to read and write the data back and forth.
Therefore, I just created my own proprietary format for exchanging data!
It makes me cry whenever I need to deal with unknown, obscure and arbitrary sequence of data blobs. It's always good to remember why we should not use unknown formats:
Reinventing the wheel is counter-productive. It seems not, but on the middle term it is. You can adapt your project to other mediums (server-side, other platforms) easily.
Using off-the-shelf components help you scale your code later.
Whenever you need to adapt your solution to other technologies and mediums, you'll work faster. Otherwise, you would probably end up with ad hoc code solutions that are not (easily) extensible and interoperable.
Using off the shelf components enables you to leverage advances in that particular technology. That's particularly important when you are using Android APIs, as they are frequently optimized for performance later down the road (See Android's Designing for Performance). Rolling your own standards may result in a performance penalty.
Unless you document your protocol, it's extremely easy to forget the protocol you created yourself. Just give it enough time and it will happen: you'll need to relearn/remember. If you document, then you are just wasting the computational time of your brain.
You think you don't need to scale your work, but chances are you will most of the time.
When you do, you will wish you had learned how to easily and seamlessly integrate well known formats.
The learning curve is needed anyway. In my experience, when you learn, you actually integrate well known formats faster than imagining your own way of doing things.
Finally, trust your data to geniuses that take their lives into creating cohesive and intelligent standards. They know it better!
Finally, if the purpose is to avoid the typical verbosity of XML, for whatever reasons, you have several options. Right now I can think of CSV, but I'm no expert in data storage, so if you're not confortable with it, I'm sure you can find good alternatives with plenty of ready to use APIs.
Good luck!

Passing multiple sqlite rows to mysql?

I was brainstorming how I should handle this. This is how the application is going to work:
User enters multiple pieces of data into app
App stores data into SQLite database
User hits SYNC button and app will pass all new/updated/deleted info to site PHP and update MYSQL.
I was thinking I can do a loop where it sends (and receive) one row at a time to mysql or I can use a string builder to build a XML, and pass (and receive) the XML string to PHP to process. The xml will have tag data specifying if the element is to be added, deleted or updated.
I figure the XML is a better option, but I'm coming here for opinions how I should push multiple rows to be added/deleted or updated to my MYSQL because I feel there's probably a more efficient/easier way of doing this.
Thanks!
--UPDATE--
Here's some helpful links I found of JSONArrays for those seeking similar information as I am about Android SQLite to PHP MYSQL.
Nice tutorial about JSONArrays in PHP: http://webhole.net/2009/08/31/how-to-read-json-data-with-php/
Another tutorial about JSONArrays in Java: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
Ah, opinions. Painful things though: everyone has one and everyone thinks their opinion is better than the next persons'.
I've implemented a system that is pretty much identical: I used JSON for it though. There is no intrinsic issue with using XML: whatever translation layer you are comfortable with is probably fine. JSON was (for me) a bit more compact than XML, required less code on both sides (json_decode is your friend) and seemed to me to be an easier row to hoe than using XML. However PHP's simplexml would probably work fine as well.
If you're doing this from scratch you might want to look at one of the systems with automatic data syncing like Mobile Couchbase (see http://www.couchbase.com/products-and-services/mobile-couchbase): would require a fair bit more tooling and a bigger server/client resource footprint but might get you there faster.

Which is the best way to fetch/retrieve data from server in android? Is it Xml or Json

I am trying to upload & retrieve data with the server.
Which is the best way to retrieve data using xml or json ?
Thank you
As i have used both XML and JSON, and also used all parser including SAX, DOM, Pull Parser. I have also developed web-service for JSON and XML both.
So I suggest you to go with JSON. why?
Because webservice for JSON response seems to develop easily, we don't need to do anything for creating JSON response, we just have to do json_encode() in PHP.
And while in Android, we can parse the JSON string easily by writing less code.
since it supports both.It depends on your feasibility.better go with json as it is easy to implement
I've just done an app that exchanges data over the wire with an API using Google's protocol buffers. There's a neat Java library available from Google for generating the java objects based on your proto files and other libraries to use for parsing an input stream into objects.
Really fast, very low bandwidth, though a bit of an overhead to setup and there's no readable data that you can drop into Notepad to view if you're having trouble.
IMHO the point of choosing one of them goes mainly down to the data size that needs to be transferred to the client. Obviously that should be as small as possible and so the preferred choice is usually
JSON
Google Protocol Buffers
..because they are much more concise than XML.
For data-oriented applications, I prefer JSON to XML due to its simplicity and ease of processing on the client side. XML may be great on the server side, but JSON is definitely easier to deal with on the client side.
have a look on the following url
http://www.subbu.org/blog/2006/08/json-vs-xml
Simplicity
XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.
Extensibility
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
Interoperability
JSON has the same interoperability potential as XML.
Openness
JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.
XML is human readable
JSON is much easier for human to read than XML. It is easier to write, too. It is also easier for machines to read and write.
XML vs JSON
JSON is better then XML that's it.

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