簡體   English   中英

XML文件的HTTP請求

[英]HTTP request for XML file

我正在嘗試將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);

                }

采用字符串的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);

我使用了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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM