我试图从google地理编码api获得xml响应。通过在浏览器中访问下面的链接,您可以看到xml响应运行得很好。但是,我的Document.parse()返回一个null文档,当打印响应实体的contentLength时,我得到-1。我发送的地址语言是希伯来语,但这似乎不是问题,因为在浏览器中,响应返回得很好。有谁能理解发生了什么事,以及为什么它不能像预期的那样工作?
String url = "https://maps.googleapis.com/maps/api/geocode/xml?"
+ "address=" + URLEncoder.encode(address, "UTF-8");
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
System.out.println(url);
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request, localContext);
System.out.println(Arrays.toString(response.getAllHeaders()));
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
doc = builder.parse(in);
System.out.println(doc.getTextContent());
return doc;我的指纹是(你可以检查链接的有效性):
https://maps.googleapis.com/maps/api/geocode/xml?address=%D7%94%D7%A2%D7%9C%D7%99%D7%94+%D7%94%D7%A9%D7%A0%D7%99%D7%94+20+%D7%90%D7%96%D7%95%D7%A8
[Content-Type: application/xml; charset=UTF-8, Date: Mon, 03 Nov 2014 19:20:50 GMT, Expires: Tue, 04 Nov 2014 19:20:50 GMT, Cache-Control: public, max-age=86400, Vary: Accept-Language, Access-Control-Allow-Origin: *, Server: mafe, X-XSS-Protection: 1; mode=block, X-Frame-Options: SAMEORIGIN, Alternate-Protocol: 443:quic,p=0.01, Transfer-Encoding: chunked]
null发布于 2014-11-03 19:38:56
好的,回答我自己的问题
最后我用了
EntityUtils.toString(response.getEntity())并且将respone类型更改为json,但我仍然无法理解为什么document.parse()不能很好地工作,因为使用了上面的行并且仍然请求xml它返回了正确的字符串
发布于 2014-11-03 19:42:16
好的是,您的医生似乎不是空的。尝试getFirstChild或类似的。GetTextContent将失败,因为根元素中很可能没有文本作为值。
https://stackoverflow.com/questions/26721627
复制相似问题