XML 文件的 HTTP 请求
我正在尝试在 Android 上为我的程序使用 Flurry Analytics,但在从服务器获取 xml 文件本身时遇到问题。
我已经很接近了,因为在 Log Cat System.out 标记中,由于某种原因,我可以获得一半的内容,它显示“XML Passing Exception = java.net.MalformedURLException:未找到协议:?xml version = 1.0编码=”UTF -8" 等...直到我的 xml 代码大约一半。不确定我做错了什么,我正在发送一个带有请求接受 application/xml 标头的 HTTP get,但它无法正常工作。任何帮助很感激!
try {
//HttpResponse response = client.execute(post);
//HttpEntity r_entity = response.getEntity();
//String xmlString = EntityUtils.toString(r_entity);
HttpClient client = new DefaultHttpClient();
String URL = "http://api.flurry.com/eventMetrics/Event?apiAccessCode=????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";
HttpGet get = new HttpGet(URL);
get.addHeader("Accept", "application/xml");
get.addHeader("Content-Type", "application/xml");
HttpResponse responsePost = client.execute(get);
HttpEntity resEntity = responsePost.getEntity();
if (resEntity != null)
{
System.out.println("Not null!");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String responseXml = EntityUtils.toString(responsePost.getEntity());
Document doc = db.parse(responseXml);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("eventMetrics");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("day");
Element dayElement = (Element) nameList.item(0);
nameList = dayElement.getChildNodes();
countString = dayElement.getAttribute("totalCount");
System.out.println(countString);
count = Integer.parseInt(countString);
System.out.println(count);
count += count;
}
}
} catch (Exception e) {
System.out.println("XML Passing Exception = " + e);
}
I'm trying to use Flurry Analytics for my program on Android and I'm having trouble getting the xml file itself from the server.
I'm getting close because in the Log Cat System.out tag I can get half of it for some reason and it says "XML Passing Exception = java.net.MalformedURLException: Protocol not found: ?xml version = 1.0 encoding="UTF-8" etc... until about half way through my xml code. Not sure what I'm doing wrong, I'm sending an HTTP get with the header requesting to accept the application/xml and it's not working properly. Any help is appreciated!
try {
//HttpResponse response = client.execute(post);
//HttpEntity r_entity = response.getEntity();
//String xmlString = EntityUtils.toString(r_entity);
HttpClient client = new DefaultHttpClient();
String URL = "http://api.flurry.com/eventMetrics/Event?apiAccessCode=????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";
HttpGet get = new HttpGet(URL);
get.addHeader("Accept", "application/xml");
get.addHeader("Content-Type", "application/xml");
HttpResponse responsePost = client.execute(get);
HttpEntity resEntity = responsePost.getEntity();
if (resEntity != null)
{
System.out.println("Not null!");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String responseXml = EntityUtils.toString(responsePost.getEntity());
Document doc = db.parse(responseXml);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("eventMetrics");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("day");
Element dayElement = (Element) nameList.item(0);
nameList = dayElement.getChildNodes();
countString = dayElement.getAttribute("totalCount");
System.out.println(countString);
count = Integer.parseInt(countString);
System.out.println(count);
count += count;
}
}
} catch (Exception e) {
System.out.println("XML Passing Exception = " + e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接受字符串的 parse 方法适用于 URL 格式。在解析字符串之前,您需要将其包装在 StringReader 中。如果您可以获取 XML 作为 InputStream 并对其进行解析,那就更好了,如下所示:
The parse method that takes a string is for a URL format. You need to wrap the String in a StringReader before parsing it. It is even better if you can grab the XML as an InputStream and parse that, something like:
我使用了 HttpURLConnection,这是一个工作代码。
I used HttpURLConnection and this is a work code.