对XML文件的HTTP请求文件、XML、HTTP

2023-09-13 00:29:26 作者:梦入薄凉

我试图使用Flurry的分析我对Android程序,我遇到了麻烦的XML文件本身从服务器。

我越来越近,因为在日志猫System.out的标签,我可以得到它的一半出于某种原因,它说XML传递异常= java.net.MalformedURLException:协议未找到:?XML版本= 1.0编码=UTF-8等等,直到通过我的XML code约一半。不知道我做错了,我送一个HTTP GET的头,要求接受申请/ xml和它的不能正常工作。任何帮助是AP preciated!

 尝试{

                // Htt的presponse响应= client.execute(后);
                // HttpEntity r_entity = response.getEntity();
                //字符串的xmlString = EntityUtils.toString(r_entity);

        HttpClient的客户端=新DefaultHttpClient();
        字符串URL = "http://api.flurry.com/eventMetrics/Event?apiAccess$c$c=YHJBA13CSKTMS6XHTM6M&apiKey=6XQY729FDU1CR9FKXVZP&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";
        HTTPGET GET =新HTTPGET(URL);
        get.addHeader(接受,应用程序/ XML);
        get.addHeader(内容类型,应用程序/ XML);
        HTT presponse responsePost = client.execute(获得);
        HttpEntity resEntity = responsePost.getEntity();
        如果(resEntity!= NULL)

        {
                    的System.out.println(NOT NULL!);

                    DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();

                    DocumentBuilder的DB = dbf.newDocumentBuilder();

                    串的responseXML = EntityUtils.toString(responsePost.getEntity());
                    文档DOC = db.parse(responseXML的);
                    doc.getDocumentElement()归()。

                    节点列表节点列表= doc.getElementsByTagName(eventMetrics);


                    的for(int i = 0; I< nodeList.getLength();我++)
                    {
                        节点node = nodeList.item(ⅰ);

                        元素fstElmnt =(元)节点;

                        节点列表名单= fstElmnt.getElementsByTagName(天);

                        元件dayElement =(元件)nameList.item(0);

                        名单= dayElement.getChildNodes();

                        countString = dayElement.getAttribute(totalCount);
                        的System.out.println(countString);
                        数=的Integer.parseInt(co​​untString);
                        的System.out.println(计数);
                        数+ =计数;

                    }
        }

    }赶上(例外五){

                    的System.out.println(XML传递异常=+ E);

                }
 

解决方案

解析方法,需要一个字符串是一个URL格式。你需要分析它之前包装在一个StringReader的字符串。它甚至更好,如果你能抓住的XML作为一个InputStream和分析的,是这样的:

 字符串的uri =
    "http://api.flurry.com/eventMetrics/Event?apiAccess$c$c=YHJBA13CSKTMS6XHTM6M&apiKey=6XQY729FDU1CR9FKXVZP&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated";

网址URL =新的URL(URI);
HttpURLConnection的连接=
    (HttpURLConnection类)url.openConnection();
connection.setRequestMethod(GET);
connection.setRequestProperty(接受,应用程序/ XML);

InputStream的XML = connection.getInputStream();

DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
DocumentBuilder的DB = dbf.newDocumentBuilder();
文档DOC = db.parse(XML);
 

ajax请求xml文件,报错 XML解析错误 未找到元素

I'm trying to use Flurry Analytics for my program on Android and I'm having trouble getting the xml file itself from the server.

I'm getting close because in the Log Cat System.out tag I can get half of it for some reason and it says "XML Passing Exception = java.net.MalformedURLException: Protocol not found: ?xml version = 1.0 encoding="UTF-8" etc... until about half way through my xml code. Not sure what I'm doing wrong, I'm sending an HTTP get with the header requesting to accept the application/xml and it's not working properly. Any help is appreciated!

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=YHJBA13CSKTMS6XHTM6M&apiKey=6XQY729FDU1CR9FKXVZP&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);

                }

解决方案

The parse method that takes a string is for a URL format. You need to wrap the String in a StringReader before parsing it. It is even better if you can grab the XML as an InputStream and parse that, something like:

String uri =
    "http://api.flurry.com/eventMetrics/Event?apiAccessCode=YHJBA13CSKTMS6XHTM6M&apiKey=6XQY729FDU1CR9FKXVZP&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);