【问题标题】:HTTP request for XML file对 XML 文件的 HTTP 请求
【发布时间】:2011-03-02 00:25:33
【问题描述】:

我正在尝试在 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 代码大约一半。不知道我做错了什么,我正在发送带有请求接受应用程序/xml 的标头的 HTTP 获取,但它无法正常工作. 任何帮助表示赞赏!

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

                }

【问题讨论】:

    标签: java android xml httpwebrequest


    【解决方案1】:

    接受字符串的 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);
    

    【讨论】:

    • 太棒了!它的工作,非常感谢你,我已经为此工作了一整天!现在通过xml整理我的阅读......
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 2017-10-03
      • 2015-03-08
      • 2019-02-09
      • 1970-01-01
      相关资源
      最近更新 更多