• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android app to call a web service with aspx

Hi Experts,

I am trying to write an app which calls a web service and read the return xml of the web service.
The webservice is only an aspx file (please see in below code), it returns an xml file.

The code have is working fine when url is a xml file, however, when I change the url to an aspx page, it throws an exception when hitting db.parse(), the exception is:

org.xml.sax.SAXParseException: Unexpected token (position:DOCDECL html@3:16 in java.io.InputStreamReader@6c0487b)

I am new to Android/Java coding, I could be using wrong class to parse this type of file. would be much appreciated if someone could help.

Thanks very much in advance!


try {
URL url = new URL("http://www.examplesite.com/sample.aspx?id=111;l=something");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

Node xnd = doc.getElementsByTagName("t").item(0);
String T = xnd.getTextContent();
txtText.setText(T);

}catch (Exception e){
//// TODO: 03-Sep-16
txtText.setText("Error:" + e.getMessage().toString());
}
 
I've figured it out myself. working code here:

try {

URL url = new URL("http://www.examplesite.com/sample.aspx?id=111;l=something");

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) urlConnection.getInputStream()));

String data = in.readLine();

urlConnection.disconnect();

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(new InputSource(new StringReader(data)));
doc.getDocumentElement().normalize();

Node xnd = doc.getElementsByTagName("t").item(0);
String T = xnd.getTextContent();
txtText.setText(T);


}
catch (Exception e) {
//// TODO: 03-Sep-16
txtText.setText("Error:" + e.getMessage().toString());
}
 
Application Development thread moved to the Development forum for better exposure.:)
 
Back
Top Bottom