2011-03-02 200 views
13

我試圖在我的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); 

       } 

回答

18

parse方法接受一個字符串是一個URL格式。你需要分析它之前包裹在一個StringReader的字符串,它甚至更好,如果你能抓住的XML作爲InputStream並解析它,如:

String uri = 
    "http://api.flurry.com/eventMetrics/Event?apiAccessCode=?????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated"; 

URL url = new URL(uri); 
HttpURLConnection connection = 
    (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/xml"); 

InputStream xml = connection.getInputStream(); 

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(xml); 
+2

太棒了!它的工作,非常感謝你,我一直在這一整天都在努力!現在通過XML來理清我的閱讀... – rwarner 2011-03-02 01:47:22

0

我用HttpURLConnection,這是一個工作代碼。

URL url = new URL("...."); 
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 

httpConnection.setRequestMethod("POST"); 
httpConnection.setRequestProperty("Accept", "application/xml"); 
httpConnection.setRequestProperty("Content-Type", "application/xml"); 

httpConnection.setDoOutput(true); 
OutputStream outStream = httpConnection.getOutputStream(); 
OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8"); 
outStreamWriter.write(requestedXml); 
outStreamWriter.flush(); 
outStreamWriter.close(); 
outStream.close(); 

System.out.println(httpConnection.getResponseCode()); 
System.out.println(httpConnection.getResponseMessage()); 

InputStream xml = httpConnection.getInputStream();