【问题标题】:Can't Parse localhost XML file (NullPointerException)无法解析 localhost XML 文件 (NullPointerException)
【发布时间】:2017-01-14 13:49:09
【问题描述】:

我尝试在 android 中制作 XML 解析器,xml 文件在我的 localhost 服务器上。当我将 URL 设置为我的 localhost-ed xml 文件时出现 NullPointerException 错误,但是当我将 xml 文件上传到保管箱时,我的解析器正在工作。

错误信息:

...
Caused by: java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:47)
at com.learn.dhemas.franchise.XMLParser.getDomElement(XMLParser.java:74)
at com.learn.dhemas.franchise.fragmentLowongan$getDataOrder.doInBackground(fragmentLowongan.java:72)
...

XMLParser,java:

public class XMLParser {
    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * @param XML string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
    }

    /** Getting node value
     * @param elem element
     */
    public final String getElementValue( Node elem ) {
        Node child;
        if( elem != null){
            if (elem.hasChildNodes()){
                for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                    if( child.getNodeType() == Node.TEXT_NODE  ){
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    /**
     * Getting node value
     * @param Element node
     * @param key string
     * */
    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
}

和我的活动中的代码行(片段类)

...
 @Override
    protected Void doInBackground(Void... params) {
        //Call parser
        XMLParser parser = new XMLParser();
        //String xml = parser.getXmlFromUrl("https://drive.google.com/uc?export=download&id=0B5bM7szIFKrpY2lPRHdCUUNscjA");
        String xml = parser.getXmlFromUrl("http://localhost/GetData.xml");
        Document doc = parser.getDomElement(xml);
...

任何帮助将不胜感激

【问题讨论】:

  • 将 localhost 替换为您的 Web 服务器和文件所在的 IP 地址
  • 我试过了,也不行
  • 哪台服务器是?试试localhost:8080/GetData.xml
  • android = 一个设备 localhost = 另一个设备。 localhost 真的在运行 Web 服务器吗?
  • 服务器在我的电脑上运行,我在同一台电脑上使用模拟器来运行应用程序。那么如何告诉 android 应用程序将 localhost 指向我的本地主机服务器而不是自己指向它呢?

标签: java android xml wamp


【解决方案1】:

所以问题是当我从应用程序指向 localhost 时,它将是模拟器上的 localhost,而不是我的 Web 服务器中的 localhost。为了解决这个问题,我只需要创建一个虚拟本地网络并为我的计算机提供一个 IP 地址,这样我就可以从模拟器中访问该 IP。

【讨论】:

    【解决方案2】:

    您可以尝试在浏览器中打开网址:http://localhost/GetData.xml

    【讨论】:

    • 是的,我可以打开http://localhost/GetData.xml。将localhost 更改为127.0.0.1 也不起作用,我在模拟器上运行该应用程序
    • 在模拟器 @Dhemas 中尝试浏览器。 (localhost || 127.0.0.1) == 模拟器。您可能需要更改为其他 ip。
    • 抱歉让我感到困惑 xD 现在我明白了,当我从应用程序指向 localhost 时,它将是模拟器上的 localhost,而不是我的 Web 服务器中的 localhost。为什么这么难弄清楚我的自我哈哈。感谢您的帮助
    猜你喜欢
    • 2021-10-06
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多