2012-08-10 120 views
1

可能重複:
Unable to connect to internet in Blackberry device?黑莓HTTP請求不起作用

我試圖發送一個HTTP請求,並獲得通過黑莓的響應。我使用下面的代碼片段。

String url="http://www.google.lk/"; 
HttpConnection con = null; 
InputStream is = null; 

try { 
    con = (HttpConnection) Connector.open(url); 
    int responseCode = con.getResponseCode(); // LINE X 
    Dialog.alert(String.valueOf(responseCode)); 
} 
catch(Exception e){ 
    Dialog.alert(e.getMessage()); 
} 

但代碼從未通過LINE X。沒有錯誤。它只是等待並最終超時。可能是什麼問題呢?

在此先感謝。

+2

http://stackoverflow.com/questions/11770789/unable-to-connect-to-internet-in-blackberry -device/11772234#11772234 – 2012-08-10 18:16:56

+0

@eugen:非常感謝。 – Bee 2012-08-11 09:37:00

回答

2

您需要告訴有關您的連接類型的Http請求。

使用該代碼來訪問連接類型

public static String getConnectionString() { 

    String connectionString = null; 

    // Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR 
    // variable. 
    if (DeviceInfo.isSimulator()) { 

     connectionString = ";deviceside=true"; 
    } 

    // Wifi is the preferred transmission method 
    else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 

     connectionString = ";interface=wifi"; 
    } 

    // Is the carrier network the only way to connect? 
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 

     String carrierUid = getCarrierBIBSUid(); 

     if (carrierUid == null) { 
      // Has carrier coverage, but not BIBS. So use the carrier's TCP 
      // network 

      connectionString = ";deviceside=true"; 
     } else { 
      // otherwise, use the Uid to construct a valid carrier BIBS 
      // request 

      connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public"; 
     } 
    } 

    // Check for an MDS connection instead (BlackBerry Enterprise Server) 
    else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { 

     connectionString = ";deviceside=false"; 
    } 

    // If there is no connection available abort to avoid hassling the user 
    // unnecssarily. 
    else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) { 
     connectionString = "none"; 

    } 

    // In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ... 
    else { 

     connectionString = ";deviceside=true"; 
    } 



    return connectionString; 

    } 

    /** 
    * Looks through the phone's service book for a carrier provided BIBS 
    * network 
    * 
    * @return The uid used to connect to that network. 
    */ 
    private synchronized static String getCarrierBIBSUid() { 
    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; 
} 

使用

con = (HttpConnection) Connector.open(url + getConnectionString()); 
+0

它的工作原理。 :D非常感謝。 – Bee 2012-08-11 09:37:34