HTTP request for XML file

后端 未结 2 779
礼貌的吻别
礼貌的吻别 2020-12-05 20:04

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

相关标签:
2条回答
  • 2020-12-05 20:18

    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=?????&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);
    
    0 讨论(0)
  • 2020-12-05 20:24

    I used HttpURLConnection and this is a work code.

    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();
    
    0 讨论(0)
提交回复
热议问题