aspose file tools*
The moose likes Android and the fly likes REST/JSON or SOAP service? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "REST/JSON or SOAP service?" Watch "REST/JSON or SOAP service?" New topic
Author

REST/JSON or SOAP service?

Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Hi Satya & Dave,

It seems like we are more and more moving from a SOAP to a more RESTful world. A lot of APIs provide us with JSON responses for (easier) processing. However processing large JSON arrays consumes a lot of memory. Processing JSON is easier than processing XML responses with e.g. SAX and fortunately projects like ksoap2-android provide us with a lightweight library to process SOAP responses. But is it really lightweight?

If -- for example -- I need to setup a new service back-end for an Android application, with your experience, what would you suggest to write: A SOAP or a RESTful/JSON service?

Cheers,

Johan


Johan Pelgrim, The Netherlands
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Bill Mote
Ranch Hand

Joined: Feb 04, 2010
Posts: 44
I'm neither Satya or Dave, but I'd like to chime in on this thread because I'm very interested in varied opinions. I personally think JSON is described as lightweight due to its leanness with regards to the amount of data that must be transmitted over the wire. CPU time to process and memory to store, while not infinite, are certainly less costly than the bandwidth used -- doubly so if you consider the potential for carrier charges.

Thoughts?

Regards,

Bill Mote
Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Hi Bill,

Thanks for commenting! I hear what you are saying, however... When -- for example -- you look at the twitter API for status updates for e.g. Andy Rubin the JSON response is 6.6Kb and the XML Response is 10Kb. Of course this is a significant difference, but not drastic in my opinion. I'm more interested in terms of productivity, maintainability and readability of the code rather than less bandwidth.

Regards,

Johan
Bill Mote
Ranch Hand

Joined: Feb 04, 2010
Posts: 44
Great points and applying real world data makes it meaningful! Stated plainly you can be a proponent for XML with "the JSON response is only 1/3 smaller" or a JSON fan-boy with "the XML response is 50% larger!"

I couldn't agree more with the bullets in your final sentence regarding: maintainability & readability. Have you looked at GSON? It sure makes getting JSON into and out of Java data objects much easier! In fact, I've written a small routine that lets me cache a data object in app preferences with .toJson(obj) and .fromJson() GSON methods.

I hope others, including Satya and Dave join in the discussion.

Bill
Satya Komatineni
author
Ranch Hand

Joined: May 11, 2011
Posts: 38
Thank you for a wonderful question. Dave is perhaps the better expert on this.

However here is my short answer.

Android so far in all the releases hasn't made an explicit effort to support SOAP. Nor does it support explicit Java/XML binding. Even for its internal work of converting the declarative UI (defiend in XML) to corresponding Java objects is done through a plain fast SAX parser.

So REST/JSON has been a preferred method for newly architected solutions. Android comes with org.json package. There are libraries that support this. The most popular is GSON. However a library called Jackson is gaining fast ground as GSON seem to be very slow. I recommend that you give Jackson a try.

On the SOAP front, a variant of SOAP called ksoap, looks like has been ported to android. You may want to look up ksoap site to see if it is compatible to call existing soap services. Some folks are using SOAP UI to generate a SOAP XML stub/template and then filling in, simulating a SOAP call through http client. There seem to be some home grown/commercial soap clients such as wsclient. The commerical "wsclient" seem real nice and seem to be architected better for Android.

I have posted my research on this topic at my site

Read this link for more on this topic

My Web Site: satyakomatineni.com
Our Book for Tablets/Phones: Pro Android 3
Bill Mote
Ranch Hand

Joined: Feb 04, 2010
Posts: 44
I found an example application using Jackson if anyone is interested: http://w2davids.wordpress.com/android-json-parsing-made-easy-using-jackson/. JavaCodeGeeks also had a great GSON/JSON tutorial that I hardened and put on a background thread. It can be found here (for code comparisons sake): http://www.javacodegeeks.com/2011/05/android-json-gson-revisited.html

I might do a Jackson implementation of the JavaCodeGeeks article if time permits.

Bill Mote
Satya Komatineni
author
Ranch Hand

Joined: May 11, 2011
Posts: 38
Johan,
I may not have reflected on the partial intent of your original question.

I personally prefer REST due to its simplicity and openness of transport.

However SOAP has the nicety of closely reflecting the idea of a Java "interface" with multiple typed methods and also a discovery of this interface.

Still I prefer REST from maintainability and agility.

Due to SOAP's complexities of implementation newer innovations may take longer to adapt to SOAP.

I like REST and let me chose a protocol that best suits me on top.

Satya
Dave MacLean
author
Ranch Hand

Joined: May 10, 2011
Posts: 49

Great thread! Well, to add my two cents worth, I would choose JSON over SOAP. I also like the Jackson parser because it is a lot faster than the built-in Android JSON. Here's an interesting post on benchmarks:

http://www.martinadamek.com/2011/01/31/comparison-of-json-parsers-performance-on-android/

I think the other thing to consider is whether or not you're going to want to reuse your services with a web-based interface. That'll be a lot easier with JSON than with SOAP. One other point I'd like to make is that if you're dealing with an existing SOAP service that you cannot change, you can always proxy it with another server-based service, so your Android client talks to your service using JSON and your server talks to the SOAP service.

- dave

Dave MacLean our book (Pro Android 4)
Johan Pelgrim
Ranch Hand

Joined: Jul 07, 2003
Posts: 105

Thanks Satya, Dave and Bill for a couple of interesting answers and links!!! JSON / Jackson it is

Found a nice benchmark which indicates Jackson is indeed much faster then GSON (which is like... the slowest on many tests )

https://github.com/eishay/jvm-serializers/wiki

Cheers,

Johan
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

just a question from a newbie in android development, if your app has to use .NET web service's, which most of then just use common SOAP objects, what is the way to go from there. For example the web services i found were sending serialized objects, and i deserialize them in java code and create the object back again. the object is JSON .
Dave MacLean
author
Ranch Hand

Joined: May 10, 2011
Posts: 49

If you're stuck with SOAP web services, you have a few options:

1) Write the code yourself to create and parse the XML required to interface with the service. This can be a lot of work but it's certainly doable. It sounds like you're doing this. There's nothing wrong with it, but it takes time.

2) Use a package such as ksoap2-android.

3) Write a proxy web service on a server somewhere that sits between your app on Android devices and the final SOAP web service. Your app could talk whatever you want to your proxy web service, and your proxy web service can do the SOAP interfaces with the SOAP web service. Obviously this adds a major component to the mix, but may offer other benefits if you wanted to implement a subscription model for instance.

- dave
 
 
subject: REST/JSON or SOAP service?