Google Places API からチェックイン場所を取得しようとしていますが、url.openStream() を介して読み取ろうとすると、XML FileNotFoundException のエラーが発生します。ブラウザの他の URL で使用しようとしたため、キーは正しいです。誰かがこの機能を使用している場合は、これを行う方法を教えてください。これが私のコードです:
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;
}
ブラウザで直接 URL を使用すると、次のエラーが発生します。
400: Your client has issued a malformed or illegal request. That’s all we know.
JSONの解析がわからないので、XMLを使用しています。出力は次のようになるはずなので、参照の値を取得しようとしています:
<CheckInRequest>
<reference>place_reference</reference>
</CheckInRequest>
更新: HttpResponse を使用して、次のように Entity に応答を取得しようとしました
if (httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity());
}
しかし、その後、 MalFormedException が発生します
doc = builder.parse(strResult); // doc is Document type
どちらの方法でも、xml結果をdocに取得するにはどうすればよいですか