XML文件的HTTP请求

rwa*_*ner 13 java xml android httpwebrequest

我正在尝试将Flurry Analytics用于Android上的程序,但我无法从服务器获取xml文件.

我已经接近了,因为在Log Cat System.out标签中我可以出于某种原因得到它的一半,它说"XML传递异常= java.net.MalformedURLException:未找到协议:?xml version = 1.0 encoding ="UTF -8"等等......直到我的xml代码大约一半.不知道我做错了什么,我发送一个带有头的HTTP get请求接受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);

                }
Run Code Online (Sandbox Code Playgroud)

bdo*_*han 21

采用字符串的parse方法用于URL格式.在解析之前,您需要将String包装在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);
Run Code Online (Sandbox Code Playgroud)

  • 真棒!它正在工作,非常感谢你,我一整天都在努力!现在通过xml理清我的阅读... (2认同)