2011-03-02 136 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();