我試圖在我的Android程序上使用Flurry Analytics,並且無法從服務器獲取xml文件本身。XML文件的HTTP請求
因爲在Log Cat System.out標籤中,我可能會因爲某種原因得到它的一半,它說「XML傳遞異常= java.net.MalformedURLException:未找到協議:?xml version = 1.0編碼=「UTF-8」等...直到通過我的XML代碼的一半。不知道我做錯了什麼,我發送一個HTTP請求與頭請求接受application/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);
}
太棒了!它的工作,非常感謝你,我一直在這一整天都在努力!現在通過XML來理清我的閱讀... – rwarner 2011-03-02 01:47:22