我一直在尝试通过 DocumentBuilder.Parse
解析一些 XML:
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 中运行良好并返回文档
。
但是,在 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;
最佳答案
(抱歉,此评论失控了!)
你的 JRE 是什么?另外,您可以粘贴此类的导入吗?从根本上来说,两个 IDE 都在同一个 JVM 上运行字节码,因此这对我来说听起来像是构建路径问题(缺少 jar/备用源)。您应该看到异常命中(尽管我将 Throwable 子类化,而不是捕获中的 Exception 子类)。
http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream) 。检查您的输入源是否为空源。 IllegalArgumentException 应该打印在 null 上,后面跟着 (in
查看您的代码:
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.
}
我建议您的工厂不是您认为的工厂,或者您收到了 Error 类,并且代码之外的某些内容正在吞没该异常。您的构建路径很有可能设置不同。
关于java - 为什么 DocumentBuilder Parse 在 Eclipse 中可以正常工作,但在 IntelliJ IDEA 中却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18049449/