2011-08-05 68 views
1

我試圖抓住一個XML文件,有以下分析:黑莓的HttpConnection超時

private void getAndParseXML(String _xmlurl) { 
     HttpConnection xmlcon = null; 
     InputStream input  = null; 
     SAXParserFactory spf = null; 

     try { 
      xmlcon = (HttpConnection)Connector.open(_xmlurl, Connector.READ); // open connection to XML source 
      spf = SAXParserFactory.newInstance();        // set up xml parsers 
      input = xmlcon.openInputStream();         // set up input stream 

      SAXParser saxparser = spf.newSAXParser(); // create a new parser object 
      saxparser.parse(input, this);    // parse operations start here 
     } 
     catch(IOException ex) { 
      System.out.println("IOException Caught:\t" + ex.getMessage()); // set a default item if any exception occurs with retreiving or parsing XML file 
     } 
     catch (SAXException ex) { 
      System.out.println("SAXException Caught:\t" + ex.getMessage()); 
      ex.printStackTrace(); 
     } 
     catch (IllegalArgumentException ex) { 
      System.out.println("IllegalArgumentException Caught:\t" + ex.getMessage()); 
      ex.printStackTrace(); 
     } 
     catch (ParserConfigurationException ex) { 
      System.out.println("ParserConfigurationException Caught:\t" + ex.getMessage()); 
      ex.printStackTrace(); 
     } 
     finally { 

      if (input != null) { 
       try { 
        input.close(); // attempt to close all connections 

       } 
       catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      if (xmlcon != null) { 
       try { 
        xmlcon.close(); 
       } 
       catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
    } // END ---------------------------------------------------------------------------- 

但我得到拋出話說,連接12秒後超時異常。這行後面執行input = xmlcon.openInputStream();

如果這是相關的,則IOException將被捕獲,並且在調用此方法之前確定是否存在活動的網絡連接。我錯過了什麼?

編輯:只是爲了澄清,這將是應用程序中的網絡連接的第一個實例。

private boolean isConnectedToNetwork() { 

     boolean isConnected = false; 

     if ((TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI))) 
      if ((TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_CELLULAR)) || (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI))) 
       isConnected = true; 

     return isConnected; 
    } 

,以確保連接是可能的,試圖檢索XML文件之前:這個代碼塊,一個簡單的測試之前。

回答

1

發現此問題。在這種情況下,url需要附加";deviceside=true"以確保建立直接的TCP/IP連接。這確保HttpConnection通過蜂窩網絡進行。換句話說,要確保連接不是通過Blackberry MDS完成的。

此外,需要一個檢查:

if ((TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)) && (TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI))) 

的Wi-Fi天線是在。如果以上評估爲true,則url(還有_xmlurl)需要進一步附加";interface=wifi"以避免蜂窩網絡,但仍然打開直接的TCP/IP連接。

1

邁克,一切都看起來不錯。

但是這裏有一些想法想:

  1. 你能從你的瀏覽器中打開URL?
  2. 您使用什麼BB傳輸打開連接(例如,它可能在BES上失敗,但在直接TCP或Wi-Fi上可以正常工作)?
  3. 在此次調用之時,讓「確定是否存在活動網絡連接」的代碼關閉了在檢測期間可能打開的所有連接(如果有的話)?
+0

是的URL可以在瀏覽器中打開。我不確定你是什麼意思的BB運輸。 –

+0

我還沒有設置在BES上運行(除非這是默認設置)。它應該只是通過單元天線或Wi-Fi的TCP/IP。在這個應用程序中,這將是連接打開的第一個實例。 –

+0

好的,我看到您只使用DirectTCP或WiFi BB傳輸。好吧,我沒有看到任何可能妨礙WIFI工作的事情,而DirectTCP可能有一些問題(例如,某些提供商可能通過設置通過連接發送的最大允許數據量來限制流量)。所以我的建議是強制使用WiFi來查看它是如何使用WiFi BB傳輸的。 –