HTTP request for XML file HTTP request for XML file android android

HTTP request for XML file


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


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