1

可能重复:
无法在黑莓设备中连接到互联网?

我正在尝试发送一个 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。没有错误。它只是等待并最终超时。可能是什么问题呢?

提前致谢。

4

1 回答 1

2

您需要告诉 Http Request 您的连接类型。

使用此代码访问连接类型

    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());
于 2012-08-10T18:11:30.857 回答