对 XML 文件的 HTTP 请求
2022-09-04 19:28:04
我正在尝试在Android上使用Flurry Analytics作为我的程序,但我无法从服务器获取xml文件本身。
我正在接近,因为在Log Cat System.out标签中,由于某种原因,我可以得到它的一半,它说“XML传递异常= java.net.MalformedURLException:协议未找到:?xml版本= 1.0编码=”UTF-8“等...直到我的xml代码大约一半。不确定我做错了什么,我正在发送一个HTTP get,标头请求接受应用程序/ xml,但它无法正常工作。任何帮助是值得赞赏的!
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);
}