I have a WCF REST service built with C# and it returns an image as part of a CPU intensive operation. The client is running on Android (Java) By default, it will return a text JSON object that looks something like this:
{"d",[9,0,77,12,11,...]}
Those are they bytes of the image. Fine. However, all the solutions for decoding this JSON are intolerably slow. I've tried Gson, Jackson, and the built-in Android JSONObject class. I have no idea why they are so slow.
As an alternative solution, I have my REST service return a GUID, and then that GUID can be used by the Android client to go to a regular URL that serves up the image as a regular binary stream, via an MVC controller.
This works well, and it fast, and is pretty easy to handle on the Android side. However, it does feel like a bit of kludge and kind of a violation of the REST design principles.
Am I missing something here? Is there a better way to do this?
How about your REST service return a Redirect 303 with a Location header that has an URL that points to the image? Or why not just return the bytes directly from the first URL?
As far as RESTful or not, returning a JSON encoded image is not exactly in the spirit of the REST self-descriptive constraint.
Just make sure the endpoint that returns the image stream of bytes, actually uses an image/* media type in the content header.
Well, on of your main problems is trying to transmit binary data using a text format.
Most if not all java json libraries will try to recognize the type of the field. It'll take a long time if there's a lot of fields.
Yeah, streaming it directly is a lot faster. Maybe you can use XML since it supports binary or blob data.
As Darrel wrote above, if the URL computes and returns an Image, simply return that Image with an appropriate content-type, for e.g., as a PNG image. Transmitting the image encoded within JSON is a strange choice, to say the least.
There is a great talk about developing rest client application on android form Google IO 2010.
http://www.youtube.com/watch?v=xHXn3Kg2IQE
This session will present architectural considerations for developing RESTful applications on the Android platform. It focuses on design patterns, platform integration and performance issues specific to the Android platform.
A great resource and must watch.
Related
Suppose you have a common communication protocol between your app and webservice. This protocol never changes and has a high demand.
I was thinking, to get a better performance use ASN.1(or other in-house bit code protocol) protocolo to code all information inside Android before send to webservice.
Today, I use json encode, and after I gzip all information before send to the webservice. I have the cost of zipping and unzipping the data, more json_decode and encode for both sides.
I know that to code to ASN.1 I will have bit operations to code and decode, but generally they are really fast. And other point that I am planning to NOT zip this information, once I am using bit coding the size will be reduced naturally.
An important observation is that I want to get a better perfomance to the server side. The client performance issue is not a problem here.
I have been trying to reduce the json data footprint that we are using in our android native application the file sizes for which are generally huge.
This has led me to believe that to achieve optimisations for json the following can be done
1) json minimization
2) compression algorithm like gzip or lzw (not sure which one will be better)
3) horizontal delta compression wherein we send only the difference/delta between two subsequent json files that greatly reduces the amount of data we send over the wire.
Please share your views if you guys have done or experimented with any of these techniques for json compression. Also we use node.js as the backend for the server side apis.
As for delta compression, you may want to look at http://jsonpatch.com
JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. When used in combination with the HTTP PATCH method it allows partial updates for HTTP APIs in a standards compliant way.
As you're using Node, you can take a look at JSON-Patch which will not only apply patches to a document/object but also has the ability to observe an object and generate a patch for you once it's been modified.
I need to send a object containing 256kb to 18Mb of data from an android device to a WCF service.
I just found out about protobuf.net and agree this is the way to go.
Most examples I find showing use of protobuf on android are designed to make queries like:
www.webservice.com/API/MaxItems=10.
What I need is a way to sent entire BLOBs to my webservice, not just parameters.
Can anyone point me in the right direction for coding this using android 2.3.3 and WCF.Net?
My scenario is as follows:
Webservice is serving clients using .Net and Silverlight and also clients on the android platform.
Webservice will serve data from filesystem and SQL to clients using protobuf.
Data beeing served will contain basic types and smaller binary chunks. 4-50Kb as protobuf objects.
There will be 17 functions taking parameters of basic types and 6 functions taking protobuf objects as parameters. One of the functions will take a protobuf object having 4 byte[] summing up to a maximum of 18Mb.
The characteristics of the served data makes protobuf a perfect match.
One of the requirements on the WebService API is to hold all these functions in one interface.
Personally, I think your best bet in that scenario (with android etc) would be to use regular in-memory serialization to create a BLOB, and then use whatever tools are available (http-post, WSE, sockets, whatever) to send that BLOB, and deserialize at the other end. Getting WCF to own the serialize/deserialize is a huge pain, especially if you aren't using full .NET. This approach should also make it pretty easy to switch between transport types entirely if needed, as you aren't really asking WCF to do much.
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.
I have an android application, that will call a SOAP web service for some data.
For the purpose of this question, we'll assume its data about cars.
I've got as far as returning text data about the car (make, model etc), that is the easy part. I'd also like to return an image of the car some how.
So far, I see two viable options :
Retrieve the image in the SOAP response (don't even know if this is
possible)
Retrieve a URL in the SOAP response that points to the image,
perhaps a URL to a web image
Could anyone please give advice as to what is a robust solution for achieving this?
Thanks
Option 2, definitely. If you do option 1, you're limited to displaying only after you've completely downloaded the entire message, including the encoded images. That can take a while. On the other hand, if you just include URLs, you can download the message, render your UI, then download the images and update those image views dynamically as the images finish fetching from the web. This results in a much more responsive UI, especially in a mobile environment where your data connection may not be as fast or reliable.