0

如果連接了互聯網但瀏覽不正常,在黑莓jre 5.0中使用平穩服務時,應用程序沒有響應。如果有任何方法設置響應超時,請引導我嗎?在Blackberry中使用網絡服務時未捕獲的異常

我正在使用此代碼來使用服務。

static HttpConnection con = null; 
static InputStream is = null; 
static StringBuffer rawResponse; 

public static String Call(String url) throws IOException { 
    try { 
     url = url + getConnectionString() + ";"; 
     if (CheckConn()) { 
      con = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true); 
      is = con.openInputStream(); 
      byte[] responseData = new byte[10000]; 
      int length = 0; 
      rawResponse = new StringBuffer(); 
      while (-1 != (length = is.read(responseData))) { 
       rawResponse.append(new String(responseData, 0, length)); 
      } 
     } else { 
      Status.show("Internet service is not avaiable."); 
     } 
    } catch (IOException ex) { 
     Status.show("Internet is not responding"); 
    } finally { 
     try { 
      is.close(); 
      con.close(); 
     } catch (Exception e) { 
      Status.show(e.getMessage()); 
     } 
    } 
    return rawResponse.toString(); 
} 

public static String getConnectionString() { 
    String connectionString = ""; 
    if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 
     connectionString = ";interface=wifi"; 
    } 
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { 
     connectionString = ";deviceside=false"; 
    } 
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 
     String carrierUid = getCarrierBIBSUid(); 
     if (carrierUid == null) { 
      connectionString = ";deviceside=true"; 
     } else { 
      connectionString = ";deviceside=false;connectionUID=" + carrierUid + ";ConnectionType=mds-public"; 
     } 
    } 
    else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) { 
     connectionString = ";None"; 
    } 
    return connectionString; 
} 


public static String getCarrierBIBSUid() { 
    net.rim.device.api.servicebook.ServiceRecord[] records = ServiceBook.getSB().getRecords(); 
    int currentRecord; 
    for (currentRecord = 0; currentRecord < records.length; currentRecord++) { 
     if (records[currentRecord].getCid().toLowerCase().equals("ippp")) { 
      if (records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0) { 
       return records[currentRecord].getUid(); 
      } 
     } 
    } 
    return null; 
} 
+0

你調試你的代碼? – Signare 2012-08-13 05:58:26

+0

是的,但我面臨同樣的問題。 – 2012-08-13 06:12:52

+0

你可以在瀏覽器上訪問互聯網嗎?用你的瀏覽器運行url – Signare 2012-08-13 06:26:52

回答

0

使用它

url = url + getConnectionString() + ";ConnectionTimeout=25000"; 

這裏超時以毫秒爲單位。一旦超時發生,它可以在catch塊中處理。

+0

我仍然面臨同樣的問題 – 2012-08-13 07:14:15

+0

它在connectionTimeout的時間應用程序終止後無法在catch塊中處理,並顯示一條消息「Uncaught Exception app terminated ...」 – 2012-08-13 07:30:14