我有一个例程从https web服务获取响应。第一次调用例程时,它可以很好地工作,返回我可以处理的xml。下一次调用它时,使用完全相同的参数,它将返回一个空文档,稍后会导致错误。如果我再次调用例程,它就会起作用 - 事实上,它似乎每隔一次调用它时就会返回空文档。我想也许我没有正确关闭urlConnection,但它在代码中看起来很好。第二遍urlConnection.getInputStream()返回空文档
我能想到的唯一的另一件事是,从异步查询中的postExecute事件调用例程。尽管如此,仍然只有一个查询发生,所以没有别的事情可以与之冲突。
代码下面的例子:
private VEDResult LookupReg(String RegNo)
{
HttpURLConnection urlConnection = null;
InputStream in = null;
VEDResult VR = new VEDResult();
try
{
// Create the URL
//
URL url = null;
url = new URL("https://<path>/getved.php?vrm=" + RegNo);
// Open the URL connection
//
urlConnection = (HttpURLConnection) url.openConnection();
// Fetch the data from the server
//
in = new BufferedInputStream(urlConnection.getInputStream());
// Set up document builder for creating the XML document
//
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
// Create the XML document from the data we received
//
Document doc = db.parse(in);
doc.getDocumentElement().normalize();
in.close();
in = null;
// Get the individual nodes from the document and assign them to the result record
//
NodeList MakeNodes = doc.getElementsByTagName("MAKE");
if (MakeNodes != null && MakeNodes.getLength() != 0)
VR.Make = MakeNodes.item(0).getTextContent();
NodeList ModelNodes = doc.getElementsByTagName("MODEL");
if (ModelNodes != null && ModelNodes.getLength() != 0)
VR.Model = ModelNodes.item(0).getTextContent();
NodeList EmissionsNodes = doc.getElementsByTagName("CO2EMISSIONS");
if (EmissionsNodes != null && EmissionsNodes.getLength() != 0)
VR.Emissions = EmissionsNodes.item(0).getTextContent();
NodeList CostNodes = doc.getElementsByTagName("VED12MONTHS");
if (CostNodes != null && CostNodes.getLength() != 0)
VR.Cost = CostNodes.item(0).getTextContent();
NodeList RegNodes = doc.getElementsByTagName("VRM");
if (RegNodes != null && RegNodes.getLength() != 0)
VR.RegNo = RegNodes.item(0).getTextContent();
NodeList BandNodes = doc.getElementsByTagName("VEDBAND");
if (BandNodes != null && BandNodes.getLength() != 0)
VR.VEDBand = BandNodes.item(0).getTextContent().toUpperCase();
}
catch (Exception e)
{
Log.v("fs", e.toString());
}
finally
{
// Tidy up
//
urlConnection.disconnect();
urlConnection = null;
}
return VR;
}
刚刚试了一下 - 没有喜悦恐怕仍然有同样的问题... – Bermudabob 2012-02-10 11:02:23