While this article includes code snippets only, the full code sample is included in dot42 update 1.0.0.61 or higher. See folder [MyDocuments]\dot42\Samples\Various\AirportInfo.
Send a GET request to the Airport Service
To get the airport status for airport OAK, including known delays and weather data, you would make a GET request to the following URL:http://services.faa.gov/airport/status/OAK?format=application/json
The above URL encodes two input arguments: First, the three letter airport code (in this case OAK). Second, the format of the data that you would like to get back (in this case JSON).
The following code makes this request:
string iataCode = "OAK"; var uri = string.Format("http://services.faa.gov/airport/status/{0}?format=application/json", iataCode); var client = AndroidHttpClient.NewInstance("AirportInfo"); var request = new HttpGet(uri); var response = client.Execute(request); // send the request
Parse the HTTP response
When the above request is made to the service, it will return a response consisting of plain text formatted according the JSON format. Android offers class JSONObject to parse the name/value pairs.First, we must extract the JSON string from the response:
var content = response.GetEntity().GetContent(); var reader = new BufferedReader(new InputStreamReader(content)); var builder = new StringBuilder(); string line; while ((line = reader.ReadLine()) != null) { builder.Append(line); } string jsonString = builder.ToString();Then we wrap a JSONObject around the string:
JSONObject json = new JSONObject(jsonString);And now we can query named values like this:
string state= json.GetString("state"); string city = json.GetString("city");
The full code
The full code sample is included in dot42 update 1.0.0.61 or higher. See folder [MyDocuments]\dot42\Samples\Various\AirportInfo.
No comments:
Post a Comment