google API request and response for windows phone with C#
Hello everyone. I have a problem trying to develop a windows phone app as a university project. Part of it is to use google APIs. So I'm trying to make a test web request.My code is as follows:
string api_request = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AIzaSyDpXZSLtOeGLJcYwKKJs2yAAEdfLST0ZXs";
HttpWebRequest google_request = (HttpWebRequest)WebRequest.Create(api_request);
HttpWebResponse google_response_file = (HttpWebResponse)google_response.GetResponse();
StreamReader google_response_datastream = new StreamReader(google_response.GetResponseStream());
As you can see I'm creating an HttpWebRequest object and I initialize it. However, when I try to get a response I create an HttpWebResponse object but the initializer method is not accessible. After some googling I find out that insted of the HttpWebResponse initializer I have to use GetResponse() instead. Everywhere I look I find this but the C# compiler keeps considering the GetResponse method non-existent (and it is). The MSDN Documentation is completely useless and not updated whatsoever concerning the WebResponse class and its child-classes. Please help me with an up-to-date method or example :-) thank you.
SpyrosMet
Junior Poster in Training
55 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 0
Where does 'google_response' come from and why aren't you calling GetResponse() on the 'google_request' object, just like documentation at MSDN says?
Momerath
Senior Poster
3,728 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13
Sorry, I forgot to copy it here correctly. the line is:
HttpWebResponse google_response_file = (HttpWebResponse)google_response_file.GetResponse();
SpyrosMet
Junior Poster in Training
55 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 0
You have to call GetResponse() on the 'google_request' object. That will return a WebResponse which can be cast into a HttpWebResponse.
Momerath
Senior Poster
3,728 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13
I tried what you suggested but the GetResponse() method doesn't seem to exist in the class definition. Is there an alternative way to make the request and receive the data from google? (I expect an XML file to be returned)
SpyrosMet
Junior Poster in Training
55 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 0
Momerath
Senior Poster
3,728 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13
I included the namespace System.Net.WebResponse but still it's not recognized. does this have to do with me using visual studio 2010 express instead of 2012?
SpyrosMet
Junior Poster in Training
55 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 0
just System.Net, no need for the WebResponse. And no, it's existed in .NET since 1.1 at least.
Momerath
Senior Poster
3,728 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 13
I have already included the System.Net namespace but still nothing. I'm trying a different approach now. I'm requesting the data as an xml document so I am trying the following:
XmlDocument doc = new XmlDocument();
doc.Load("https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&sensor=true&key=AIzaSyDpXZSLtOeGLJcYwKKJs2yAAEdfLST0ZXs");
The problem is with the XmlDocument class. I have included the System.Xml namespace but the class name and constructor are not recognized. I'm getting really upset with visual studio. There is no explanation so far as to what is wrong with the recognition of the classes and their methods. Do I need to install an extra package of namespaces or something? (I just installed Windows 8 with visual studio 2012 express for windows 8. Is there something more I need to install?)
SpyrosMet
Junior Poster in Training
55 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
Skill Endorsements: 0