1. Download our Official Android App: Forums for Android!

Apps Android app to call a web service with aspx

Discussion in 'Android Development' started by Cool Droid, Sep 3, 2016.

  1. Cool Droid

    Cool Droid Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    2
    Joined:
    Sep 3, 2016

    Sep 3, 2016
    2
    0
    15
    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());
    }
     

    Advertisement

  2. Cool Droid

    Cool Droid Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    2
    Joined:
    Sep 3, 2016

    Sep 3, 2016
    2
    0
    15
    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());
    }
     
  3. Unforgiven

    Unforgiven OK Google
    Moderator
    Rank:
     #2
    Points:
    4,238
    Posts:
    35,576
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    35,576
    39,945
    4,238
    Male
    Douglas, MA
    Application Development thread moved to the Development forum for better exposure.:)
     

Share This Page

Loading...