Android 第二次通过urlConnection.getInputStream()返回空文档

Android 第二次通过urlConnection.getInputStream()返回空文档,android,urlconnection,Android,Urlconnection,我有一个从https web服务获取响应的例程。第一次调用该例程时,它工作得很好,返回我可以处理的xml。下次使用完全相同的参数调用它时,它将返回一个空文档,这将在以后导致错误。如果我再次调用例程,它就会工作——事实上,它似乎每隔调用一次就返回空文档。我想可能我没有正确关闭urlConnection,但代码中看起来没问题 我能想到的另一件事是,该例程是从异步查询中的postExecute事件调用的。不过,一次仍然只有一个查询,因此没有其他冲突 代码示例如下: private VEDResult

我有一个从https web服务获取响应的例程。第一次调用该例程时,它工作得很好,返回我可以处理的xml。下次使用完全相同的参数调用它时,它将返回一个空文档,这将在以后导致错误。如果我再次调用例程,它就会工作——事实上,它似乎每隔调用一次就返回空文档。我想可能我没有正确关闭urlConnection,但代码中看起来没问题

我能想到的另一件事是,该例程是从异步查询中的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;
}
private VEDResult LookupReg(字符串RegNo)
{
HttpURLConnection-urlConnection=null;
InputStream in=null;
VEDResult VR=新的VEDResult();
尝试
{          
//创建URL
//
URL=null;
url=新url(“https:///getved.php?vrm=“+RegNo);
//打开URL连接
//
urlConnection=(HttpURLConnection)url.openConnection();
//从服务器获取数据
//
in=new BufferedInputStream(urlConnection.getInputStream());
//设置文档生成器以创建XML文档
//
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
文档生成器数据库;
db=dbf.newDocumentBuilder();
//根据我们收到的数据创建XML文档
//
文档doc=db.parse(in);
doc.getDocumentElement().normalize();
in.close();
in=null;
//从文档中获取各个节点,并将它们分配给结果记录
//
NodeList MakeNodes=doc.getElementsByTagName(“MAKE”);
if(MakeNodes!=null&&MakeNodes.getLength()!=0)
VR.Make=MakeNodes.item(0.getTextContent();
NodeList ModelNodes=doc.getElementsByTagName(“模型”);
if(ModelNodes!=null&&ModelNodes.getLength()!=0)
VR.Model=ModelNodes.item(0.getTextContent();
NodeList EmissionsNodes=doc.getElementsByTagName(“二氧化碳排放”);
if(EmissionsNodes!=null&&EmissionsNodes.getLength()!=0)
VR.Emissions=EmissionsNodes.item(0).getTextContent();
NodeList CostNodes=doc.getElementsByTagName(“ved12个月”);
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();
} 
捕获(例外e)
{
Log.v(“fs”,例如toString());
}   
最后
{
//收拾
//
urlConnection.disconnect();
urlConnection=null;
}    
返回虚拟现实;
}

尝试在每次调用后进行垃圾收集

System.runFinalization();
System.gc();

在打开连接问题之前:

System.setProperty("http.keepAlive", "false");