How to parse TFL api - android

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.

Related

Json Removing Leading zeros

Quick and probably stupid question here,
I am pulling JSON data from my server and displaying it in my app in edit texts
I am using volley to GET the data, the JSON data are Dates in the fomat of MM/dd/YYYY so lets say its for ex. 03/12/2020,
but now My problem is my App displays it as 3/12/2020 and postman does the same, so the 0 goes missing but i am pulling the data directly from my SQL , which includes the 0,
I have learned that Json Removes leading 0's for various reasons,
But how can I add the 0 back to the json within android java using volley
Will Post Code if needed
AFAIK date, formatted like this, should easily be parsed so that you get object of class Date or something similar. After you have this, you can format it any way you want (depends on the object being used, though).

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.

how do i get the website in json format?

i need to get a website (any website ) in json format to use it with android and json parser to get data and if possible that the website include images to be able to download data and images using json parser.
i have been searching about 2 days without any success, its my first time that i use json so i do know where to search and how to search.
what i need is a website to make a test on it that will be available in json format
anyone can help me ???
i will appreciate that.
Use http://www.jsontest.com/ to get JSON from that website.
Read there and for example if you call http://ip.jsontest.com/ you will get a JSON response as {"ip": "203.92.62.242"} then you can parse them in your android application.
For future readers :
Facebook programmers provide the json format for each request.
All you have to do is :
If the facebook page url is : http://www.facebook.com/youtube
To get json format of page, request this : http://graph.facebook.com/youtube
I suggest to read that :
Usually standard web service today provide data in at least two format: json and xml for each endpoint (url), you just add .xml or .json extension to the requested url.
But most of the time you don't have to do that, common web services today always provide json data because it's defined in RESTFul rules. If they dont, you can't get the json. The case is same for you, just ask the developer for API docs.

server response xml or json objects 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.

Is there a simple way to tell if an arbitrary string is well-formed JSON in Android?

We're writing an app that communicates with an internet-based CMS in JSON. Unfortunately, some types of network connection e.g. through public Wifi have a web gateway that they require you to go through before you can use the net normally. This means we are trying to JSON parse an HTML webpage. I would expect it to throw an exception stating that the HTML isn't well-formed JSON but instead it tries to parse it, runs out of memory and collapses.
I have looked through the Android JSON functions and can't find a function that simply tells you if a string is JSON or not. Is there such a function? If not, am I simply going to have to write something heuristic to trap obviously non-JSON strings?
The exact way to check for its well-formed JSON is to create a JSONObject based on the string returned from HTTP Response. JSONException will occurs if the resulted string is not a well-formed JSON.
I don't know why you're running out of memory too soon. Is it because the returned string is very large? If so, you can take the advantages of the JSON format. Check the first character of the string. If it's { then it's probably JSON. Otherwise, it's not a JSON.
Please note the word "probably". It's because I don't know whether the server sometimes gives you another response with { as its first character and it's not a JSON. You decide.
You should read a JSON string formatted here.
You should be checking the Content-Type HTTP header of the response. If it is returning HTML, it will be text/html. If it is returning JSON, it will be application/json.
Telling you whether this is valid JSON or not would require complete parsing. Android bundles vanilla JSON parse working on like XML DOM parser - those parsers try to construct complete syntax tree in memory - thus they are prone to
low performance
heavy memory usage
I would suggest to use pull parser like GSON - there is also some kind of databinding which comes handy if you need objects out of your JSON or just strupped down binary ( 16KB ) to just parse ( I use it with my own databinding layer: https://github.com/ko5tik/jsonserializer )
Something like:
Header contentType = httpResponse.getFirstHeader("Content-Type");
if (contentType.getValue().compareToIgnoreCase("application/json") != 0) {
// response is not JSON
response = null;
}

Categories

Resources