HTTP请求XML文件

13

我正在尝试在Android程序中使用Flurry Analytics,但是我无法从服务器获取xml文件。

我已经接近成功了,因为在Log Cat System.out标记中,我可以获得一半的内容,但是出现了“XML Passing Exception = java.net.MalformedURLException:Protocol not found:?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);

                }
2个回答

21

接受字符串参数的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

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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接