HttpConnection connect with Mobile Network or 3G

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 09:09:34

问题


When I run this application on a device using the WiFi it's working fine. But when I am using a mobile network or 3g it's giving an error. It's not working on the mobile network.

I am using this code:

connection = (HttpConnection) Connector.open(APIURL+ updateConnectionSuffix());     

And my ConnectionTools class code:

public String updateConnectionSuffix() {
    String connSuffix;
    if (DeviceInfo.isSimulator()) {
        connSuffix = ";deviceside=true";
    } else if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
            && RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
        connSuffix = ";interface=wifi";
    } else {
        String uid = null;
        ServiceBook sb = ServiceBook.getSB();
        ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
        for (int i = 0; i < records.length; i++) {
            if (records[i].isValid() && !records[i].isDisabled()) {
                if (records[i].getUid() != null
                        && records[i].getUid().length() != 0) {
                    if ((records[i].getCid().toLowerCase().indexOf("wptcp") != -1)
                            && (records[i].getUid().toLowerCase().indexOf(
                                    "wifi") == -1)
                            && (records[i].getUid().toLowerCase().indexOf(
                                    "mms") == -1)) {
                        uid = records[i].getUid();
                        break;
                    }
                }
            }
        }
        if (uid != null) {
            // WAP2 Connection
            connSuffix = ";ConnectionUID=" + uid;
        } else {
            connSuffix = ";deviceside=true";
        }
    }
    return connSuffix;
}

Can you give me any solutions?

What should we do for the mobile network or 3g?


回答1:


Try this code.

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;
}

Replace this function by your updateConnectionSuffix().




回答2:


Let me explain : - This is for connection using mobile network 2g or 3g any network" just copy & paste & enjoy

String url = "vm.b24esolution.com:9090";

final HttpConnection connection = (HttpConnection) Connector.open("socket://"+url+updateConnectionSuffix()+";apn=rim.net.gprs;tunnelauthusername =;tunnelauthpassword=",Connector.READ_WRITE);



来源:https://stackoverflow.com/questions/11670246/httpconnection-connect-with-mobile-network-or-3g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!