I am developing an application for the Windows Phone 7. I am making a GET request to a JSON service. The return type of the request is an object.
My question is, how do I create a string from the information in the object. The code below is what I am using to make the request and process the response.
HttpWebRequest carRequest = (HttpWebRequest)WebRequest.Create(carUrl);
carRequest.Method = "GET";
carRequest.BeginGetResponse(new AsyncCallback(ProcessResponse), carRequest);
private void ProcessResponse(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
WebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
StreamReader sr = new StreamReader(response.GetResponseStream());
Car.car = (Car)JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(Car));
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate(new Uri("/SearchResults.xaml", UriKind.Relative));
});
}
Why not use a WebClient?
Note that this is a general solution for downloading a string. If you are resolving JSON, then use one of the methods included in JSON.Net for this kind of interaction.
Also note that you may want to stash your result object somewhere so it can be accessed on your search result page, or do the downloading/deserialising there.