자마린 xamarin 에서 웹서비스를 통해 json 형태 비동기로 가져오는 방법입니다.
https://developer.xamarin.com/recipes/android/web_services/consuming_services/call_a_rest_web_service/
//버튼 클릭시 호출하기
button.Click += async (sender, e) => {
//웹서비스 호출하기
string url = "http://api.geonames.org/findNearByWeatherJSON?lat=" +
latitude.Text +
"&lng=" +
longitude.Text +
"&username=demo";
// Fetch the weather information asynchronously,
// parse the results, then update the screen:
JsonValue json = await FetchWeatherAsync (url);
// ParseAndDisplay (json);
};
// json 형태로 비동기 가져오기
private async Task<JsonValue> FetchWeatherAsync (string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync ())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream ())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream));
Console.Out.WriteLine("Response: {0}", jsonDoc.ToString ());
// Return the JSON document:
return jsonDoc;
}
}
}