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.
Related
I want to develop an android application which parses json data into some meaningful data sets and accordingly display them into the UI .
I need suggestions on how to go about it.
PS : i have 0 idea of the available options to chose , so different ways to do the same would help me a lot , since i am new to it
Thanks in advance
You can use gson for parsing the data together with some network library like volley or can use retrofit which provide direct parsing with the help of gson and other lib.
To accomplish this you first need to create some relevant Java pojo
For corresponding json dataset , so that you can use it .
You can generate pojo from this website very easily for any json data
http://www.jsonschema2pojo.org/
I need to get some simple data (actual currency values) to my android application. I'm not sure what is the best way to achieve it. Do I have to make some database and put it on server and than update it manually or there is some better way?
You can use an API Service and access to this service from you android device. You donot need to do a server implementation for this.
e.g. for exchange rates check https://openexchangerates.org/
Yes.
Please make a server and a PHP which going to print out the values in JSON. You can validate your json with http://jsonlint.com/. After it you can download and parse the json with yourself or a 3rd part library (like retrofit).
You can use this website to generate POJO, so retrofit can parse the json for you by it`s own.
http://www.jsonschema2pojo.org/
select: JSON instead of scheme and select GSON instead of JSON, if you are using retrofit.
sources:
https://en.wikipedia.org/wiki/Gson
https://github.com/square/retrofit
http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/
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.
I have a .net webApi project that contains some methods.
The methods send and get complex objects.
My android application gets the objects in json format and parsing them manually.
In any changes of the objects in the WebApi project I have to change manually the android application project.
I would like to know what is the best practice to work with android application client and .Net WebApi.
There is any tool to connect between them or to auto map the objects?
Please help me
Tal
U do use json parsing technique for parsing the json responses.its proper way of data parsing there is i think no tool for auto mapping.
From the Android Client end you have to use Spring RestTemplate together with Data Transfer Objects (DTOs). You wouldn't need to manually manipulate a JSON String values. It's all about dealing with Java Objects then. You can directly pass the Objects to the RestTemplate as Entity classes and get the JSON String Auto Mapped to your DTOs.
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.