fabian
fabian

Reputation: 3

SAX-Parser throws UnknownHostException

i wrote an working XML-Saxparser a few weeks ago. For some reason, i don't know why, it isn't working anymore and i can't think of what i did on those classes. when i look into the debugger, the last thing that works is URL url = new URL("http://hammerman.bplaced.net/Objekteingabe1.xml");

the line xr.parse(new InputSource(url.openStream()));seems to throw an IOException. The Details for "e" in the catch-clause says "UnknownHostException" or "Host is unresolved: ..." stackTrace: null.

As i said, i really don't know what happened, so i think i made a stupid misstake somewhere or i deleted a line. But i searched for hours now, i just can't find it. Hope somebody can help me.

Thanks a lot. Fabian

My Readerhelper looks as following

try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        handler = new Dokukmentenhandler();
        xr.setContentHandler(handler);
        URL url = new URL("http://hammerman.bplaced.net/Objekteingabe1.xml");

        xr.parse(new InputSource(url.openStream()));
//      Textobjekte = handler.Textobjekte;
//    Phasen_anzahl = handler.Phasen_Counter;

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block

        e.printStackTrace();
    }

and my handler

@Override
  public void startDocument() throws SAXException
  {

  }

  @Override
  public void startElement(String namespaceURI, String localName, String tagName, Attributes attributes) throws SAXException
  {
      // lege ein erstes dData objekt an
      if (localName.equals("tData")) {
        aktuell = new Textobjekt();
        // damit das objekt existiert
        currentVal = "";
    }
        // alle 4 Sensorarten werden abgefangen
        if (localName.equals("Realer_Sensor")) {
            realerSensor = true;
            op = new Option();
        }else if (localName.equals("Normalverteilung")) {
            normalverteilung = true;
            op = new Option();
            op.Sensor_name= "Normalverteilung";
        }else if (localName.equals("Bernoulliverteilung")) {
            bernoulli = true;
            op = new Option();
            op.Sensor_name= "Bernoulliverteilung";
        }else if (localName.equals("Gleichverteilung")) {
            gleichverteilung = true;
            op = new Option();
            op.Sensor_name= "Gleichverteilung";
        }
  }
  @Override
  public void endElement(String namespaceURI, String localName, String tagName) throws SAXException
  {
      if (localName.equals("Variablen_Name")) {
        aktuell = new Textobjekt();
        aktuell.Variablenname = currentVal;
        Textobjekte.add(aktuell);
    }else if (localName.equals("Phasenanzahl")) {
        Phasen_Counter = Integer.parseInt(currentVal);

    }else if (localName.equals("Initialwert")) {
        aktuell.Initialwert = Float.parseFloat(currentVal);
    }
    // alle 4 Sensorarten werden abgefangen, die booleans geschlossen und die optionen hinzugefuegt
    else if (localName.equals("Realer_Sensor")) {
        realerSensor = false;
        aktuell.Optionenliste.add(op);
    }else if (localName.equals("Normalverteilung")) {
        normalverteilung = false;
        aktuell.Optionenliste.add(op);
    }else if (localName.equals("Bernoulliverteilung")) {
        bernoulli = false;
        aktuell.Optionenliste.add(op);
    }else if (localName.equals("Gleichverteilung")) {
        gleichverteilung = false;
        aktuell.Optionenliste.add(op);
    }   
    else if (localName.equals("Sensor_Name")) {

            op.Sensor_name = currentVal;

    }   
    else if (localName.equals("Parameter")) {
        if (realerSensor) {
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }

    }else if (localName.equals("Nu")) {
        if (normalverteilung) {
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }

    }else if (localName.equals("sigma")) {
        if (normalverteilung) {
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }
    }
    else if (localName.equals("Pi")) {
        if(bernoulli){
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }
    }
    else if (localName.equals("Untere_Wertegrenze")) {
        if (gleichverteilung) {
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }
    }
    else if (localName.equals("Obere_Wertegrenze")) {
        if (gleichverteilung) {
            float f = Float.parseFloat(currentVal);
            op.list.add(f);
        }
    }
    // Ende der Optionenliste  


    else if (localName.equals("Eventvorkommen")) {
        aktuell.Eventvorkommen = currentVal;
    }
    else if (localName.equals("Bezugsart")) {
        aktuell.Bezugsart = currentVal;
    }
    else if (localName.equals("Eventart")) {
        aktuell.Eventart = currentVal;
    }
    else if (localName.equals("Ueberschreitungswert")) {
        float f = Float.parseFloat(currentVal);
        aktuell.Ueberschreitungswert = f;
    }
    else if (localName.equals("Auswirkung")) {
        float f = Float.parseFloat(currentVal);
        aktuell.Auswirkung = f;
    }
    else if (localName.equals("Objektname")) {
        re = new Referenz();
        re.Objektname = currentVal;
    }
    else if (localName.equals("Referenzwert")) {
        float f = Float.parseFloat(currentVal);
        re.Referenzwert = f;
        aktuell.referenzliste.add(re);
    }


  }

  @Override
  public void characters(char[] ch, int start, int length)
  throws SAXException {
      if (length > 0) {
         currentVal = new String(ch,start,length);
    }
  }   
}

Upvotes: 0

Views: 656

Answers (4)

asdf
asdf

Reputation: 11

Remember the internet user permissions in manifest..

uses-permission android:name="android.permission.INTERNET"

Upvotes: 1

fabian
fabian

Reputation: 1

Correction by Fabian:

it seemed like Phils answer was right(too?) it worked when i did as mentioned before(delete xsi:noNamespaceSchemaLocation="Objekteingabe.xsd) but that may be happened by accident. For everyone who has the same problem: Changing the test device is a good option.

i just got the same problem again, and starting another emulator solved my problem. sorry for confusion in here!!!

Upvotes: 0

Phil Lello
Phil Lello

Reputation: 8639

Unless you've made changes while I'm typing this, the referenced URL & XSD files are correct, so not the problem (I can load them in my browser).

This is most likely to be a network connectivity issue on the test device, as it can't resolve a name for the host.

Upvotes: 0

Heiko Rupp
Heiko Rupp

Reputation: 30934

This sounds a lot like http://hammerman.bplaced.net/Objekteingabe1.xml can not be reached. Can you call that address from a browser?

If so, the 2nd option can be that the Parser sees

<Rahmeneingabe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="Objekteingabe.xsd">

and tries to load the XSD and fails.

Usually you should get the name of the host that it fails to resolve in the stack trace.

Upvotes: 0

Related Questions