我一直在尝试通过以下方式解析一些 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;