1

我一直在尝试通过以下方式解析一些 XML DocumentBuilder.Parse

    try 
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

这在 Eclipse 中工作正常并返回Document.

但是,在 IntelliJ IDEA 中运行代码命中Document doc = builder.parse(in);然后直接跳过整个 try/catch 并null直接在 try/catch 之后返回(或任何可能存在的代码)。

它不会抛出任何异常,也没有说明为什么它可能会简单地失败!

有谁知道为什么会发生这种情况?我真的不想回到 Eclipse

编辑 - 演示:

        try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in); //if we step through to here, this line executes then goes directly to "x" below
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null; //x

还尝试添加以下内容来捕获错误,但也没有命中:

            builder.setErrorHandler(new ErrorHandler() {
            @Override
            public void error(SAXParseException arg0) throws SAXException
            {
                throw arg0;
            }

            @Override
            public void fatalError(SAXParseException arg0) throws SAXException
            {
                throw arg0;
            }

            @Override
            public void warning(SAXParseException arg0) throws SAXException
            {
                throw arg0;
            }
        });

编辑 2:进口

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.google.android.gms.maps.model.LatLng;

import android.content.Context;
import android.util.Log;
4

1 回答 1

0

(对不起,这条评论失控了!)

你的JRE是什么?另外,你能粘贴这个类的导入吗?从根本上说,两个 IDE 都在同一个 JVM 上运行字节码,所以这对我来说听起来像是一个构建路径问题(缺少 jar/替代源)。你应该会看到一个异常命中(尽管我会继承 Throwable,而不是我的 catch 中的 Exception)。

http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream)。检查您的输入源是否为空源。IllegalArgumentException 应打印在 null 上,然后(在

查看您的代码:

  try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response = httpClient.execute(httpPost, localContext);
    InputStream in = response.getEntity().getContent();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); //**<--What class is this?**
    Document doc = builder.parse(in); //if we step through to here, this line executes then goes directly to "x" below
    return doc;
} catch (Exception e) { //<-- Does not catch Error
    e.printStackTrace();
}
return null; //x

尝试这样做:

try
{
  //Your code to read the document
} 
catch (Throwable t)
{
  t.printStackTrace(); //Check your output
  //Set your breakpoint HERE
  throw new RuntimeException(t.getMessage(), t); //Check the ExceptionHandler for the application.
}

我建议您的工厂不是您认为的工厂,或者您正在获得一个错误类并且您的代码之外的某些东西正在吞噬该异常。很有可能您的构建路径设置不同。

于 2013-08-05T15:15:34.573 回答