server response xml or json objects android - android

I have an application which uses restful web services, the service returns a json object.
I wanted to know , what would be better , returning a json or xml response?
Please help as i am new to the concept.

The answer is that it doesn't really matter. They are both almost equivalent. They use slightly different API's so go with the one you feel more comfortable with.
Example of JSON parsing in Android you can find in this question.
Example of XML parsing in Android you can find in this question.
The only reason I can think of to use XML over JSON is when your webservice responses are huge. JSON usually requires the entire response to arrive before you can start parsing. XML easily supports a pull parser which can start parsing "on the fly" before all the data arrives - which can be much more efficient. But if your responses are small, you don't really need all that.
If your existing webservice already returns a JSON object, this could be reason enough to stick with JSON.

Related

Android communication with server via JSON

I just got placed in an IT company. They gave me android to work on. I am having trouble deciding what to learn on the topic. The server side is using JSON objects. Can someone tell me what topics i should learn to communicate with it.
Should i learn volley or something else?
first learn about AsyncTask. (it will teach you how to get data from server) a
2) learn about JSON notation, you will come to know how to parse json

Using a custom API

I just started using the website http://www.kimonolabs.com and created an API as I need to retrieve data from my school website to put into the school app I am creating. Unfortunately, I do not really plan ahead, and this leads me to my 2 questions:
1) How do I include this API in my school Android App?
2) How do I parse the JSON in this API? (Or just linking me to another page on this would be equally as greatly appreciated :) )
Thanks very much for any help you can give! Sorry for my language, I am 14 :P
First things first. Take a look at AsyncTask in Android. AsyncTask is used to perform operations asynchronously. Take a look at this answer explaining how parameters are passed to AsyncTask.
You can use libraries mentioned bellow to do same thing:
Asynchronous Http Client
Volley
Once you communicate with server and receive response, you have to parse JSON data.
The key you have to remember for this is
[] - square bracket represents JSON Array
{} - curly bracket represents JSON Object
Everything else will be combination of these.
This tutorial will help you regarding JSON parsing.
JSON object to Java object conversion
To convert json data to java object you can use:
Jackson
Gson
Follow this link or this link for nice GSON tutorial.
I personally used GSON, and it is best library for json to java object conversion.

Using REST to Send and Receive Complex Data

My client is an Android app and my service is an asp.net web api. I’m the only one using my service. I am trying to duplicate, in the Android-REST world what I am already doing in the Microsoft Windows Phone 7/ WCF SOAP world. I have numerous methods that both receive complex objects and return complex objects.
The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
But when I post questions about doing the same thing in REST, I’m told I should limit my services into GET, PUT, POST, and DELETE only. And that I should only do what is “proper” according to RFC2616. Some speak of this in almost a religious manner.
Forgeting about the religion, what’s wrong with using a GET for everything? Or what’s wrong with using a POST for everything? What I do does not fall into the simplistic RFC2616 categories. For instance I’m passing a thousand legs of a trip taken in a car and I’m getting back another version of that trip with erratic GPS errors smoothed out. Or, I’m sending a conversation in english and getting that conversation back in German.
In the android client I have the objects I want to send over HTTP already serialized into json strings by using Google-GSON. So my questions are…
How can I send these json strings to my REST Service as arguments in either GET or POST?
Is it possible and feasible to use just all GETs (or all POSTs) for all of my calls to my REST Service and how do I do that?
I have a more pragmatic question about this posted at sending a json string in a http url as I can’t find any examples anywhere of sending json strings over http GET or POST.
Thanks, Gary
Using the good HTTP verb is very usefull to simply know what to do when you request failed (for example) or just to do some specific stuff. If you sent a POST request, it's implicitly suppose that you have to parse your resource in order to obtain a stream which be sent via the request's body. In other hand, when you are retrieving data via GET, it's suppose that the request is gonna be sent back to you as a stream that you will mapped to your model, pojo, or anything else.
I can suggest you to use library such as RESTDroid. You can send POJO and receive POJO. It's a "resource oriented" library, so you can know at any moment if a particular local resource is remotely syncronized. Data persistence between local and remote is automatically handles.
RESTDroid is alpha released. You can have a look to RoboSpice. It's a powerful library to manage REST call but it's up to you to manage the persistency between local and remote resources.
1) The WCF-SOAP world is simple. You can pass any complex arguments you want and return any complex results you want. Logically, it’s just a Remote Procedure Call.
- IN REST:"You can pass any complex arguments you want and return any complex results you want too.
2a) Forgeting about the religion, what’s wrong with using a GET for everything?
In SOAP services WCF/or classical you are wrapping all requests into http POST so using single verb would end up to SOAP or - don't even think about it - your own communication protocol:-D
2b) You can technically compose GET request with non empty body - most of the servers ignore it by default though and it would be technically problematic to read it..
the other part of the question is answered by Pcriulan above

How to parse TFL api

I am currently working on TFL based projects. And i want to parse the json file which is available in this link : http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1
So please help me out
This API is not standard JSON as written in the TFL API documentation. It us actually best parsed as CSV but watch out because I have found out that some responses are inconsistent where the first line might have 6 strings while the second might have 5 with the missing field not identified as an empty string but simply omitted. This way parsing with a CSV parser will lead to errors since you might never know what is omitted.
I have also searched for the solution and the best I could find was another API that gives standard JSON but only returns bus stop countdown via a stopCode request.
Use This Link To Access It.
http://countdown.tfl.gov.uk/stopBoard/75288
Im suprised as to why TFL uses this api for their own apps but not implement the public api to return good JSon like this one.
This API is not standard JSON as written in the TFL API documentation.so after get the content you may change that format. Use this link http://jsonlint.com/ it will validate your json format. so you can easily understand json format error
After get the content do this
JSONArray jArray = new JSONArray("["+ result.replaceAll("]", "],").toString() + "]")
now you get proper json array skip jArray 0th position this is - URA Version array.
It is not a single full JSON object, as per the TFL documentation. You treat each line as a separate JSON object. This way if/when you move to the streaming mode, you can continue to receive objects and interpreting them as they get streamed to you. Also you use the first element in the JSON array to determine how to process that particular line, or in some cases if you need to refresh the base data.

Server requests / JSON parsing

What can you recommend in terms of tools for requesting a server and parsing received JSON data? Apart from simple parsing, I might want to map those responses to objects.
Flexjson works on Android for this exact purpose.
http://flexjson.sourceforge.net

Categories

Resources