SAX 解析器抛出 UnknownHostException

发布于 2024-11-02 18:32:57 字数 5834 浏览 7 评论 0原文

几周前我写了一个可运行的 XML-Saxparser。由于某种原因,我不知道为什么,它不再起作用了,我无法想起我在这些课程上做了什么。 当我查看调试器时,最后起作用的是 URL url = new URL("http://hammerman.bplaced.net/Objekteingabe1.xml");

xr. parse(new InputSource(url.openStream()));似乎抛出 IOException。 catch 子句中“e”的详细信息显示“UnknownHostException”或“主机未解析:...”stackTrace:null。

正如我所说,我真的不知道发生了什么,所以我认为我在某个地方犯了一个愚蠢的错误,或者删除了一行。但我已经搜索了几个小时了,就是找不到。 希望有人能帮助我。

多谢。 Fabian

我的 Readerhelper 如下所示

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

,我的处理程序如下

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慈悲佛祖 2024-11-09 18:32:57

记住清单中的互联网用户权限。

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

Remember the internet user permissions in manifest..

uses-permission android:name="android.permission.INTERNET"
仄言 2024-11-09 18:32:57

这听起来很像 http://hammerman.bplaced.net/Objekteingabe1.xml 无法访问。您可以从浏览器调用该地址吗?

如果是这样,第二个选项可能是解析器看到

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

并尝试加载 XSD 但失败。

通常,您应该在堆栈跟踪中获取无法解析的主机的名称。

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.

过去的过去 2024-11-09 18:32:57

除非您在我输入此内容时进行了更改,否则引用的 URL 和XSD 文件是正确的,所以不是问题(我可以在浏览器中加载它们)。

这很可能是测试设备上的网络连接问题,因为它无法解析主机的名称。

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.

〆一缕阳光ご 2024-11-09 18:32:57

Fabian 更正:

菲尔斯的答案似乎是正确的(也是?)当我按照之前提到的那样(删除 xsi:noNamespaceSchemaLocation =“Objekteingabe.xsd”)时,它起作用了,但这可能是偶然发生的。
对于遇到同样问题的每个人:更改测试设备是一个不错的选择。

我刚刚再次遇到同样的问题,启动另一个模拟器解决了我的问题。
抱歉,这里造成混乱!

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!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文