I am trying to fetch checkin locations from Google places API but I am getting error for XML FileNotFoundException when I try to read it through url.openStream(). My key is correct because I tried using it in some other URL in browser. If someone has used this feature then please let me know how to do this. Here is my code:
public String checkingMessage()
{
String strURL = "https://maps.googleapis.com/maps/api/place/check-in/xml?sensor=true&key=" + GOOGLE_API_KEY;
URL url;
try
{
url = new URL(strURL.replace(" ", "%20"));
// Standard of reading a XML file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
XPathExpression expr = null;
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(url.openStream()));
// Create a XPathFactory
XPathFactory xFactory = XPathFactory.newInstance();
// Create a XPath object
XPath xpath = xFactory.newXPath();
// Compile the XPath expression
expr = xpath.compile("//reference/text()");
// Run the query and get a nodeset
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
String answer = nodes.item(0).getNodeValue();
Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG);
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
If I use direct url in browser, I get following error:
400: Your client has issued a malformed or illegal request. That’s all we know.
I dont know json parsing, so I am using XML. Output I believe should be like this, hence I am trying to fetch value of reference:
<CheckInRequest>
<reference>place_reference</reference>
</CheckInRequest>
Update : I tried using HttpResponse and get response into Entity as follows
if (httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity());
}
But then I get MalFormedException on
doc = builder.parse(strResult); // doc is Document type
Either ways how can I get xml result into doc