的HttpConnection与移动网络或3G连接 [英] HttpConnection connect with Mobile Network or 3G

查看:178
本文介绍了的HttpConnection与移动网络或3G连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行使用了WiFi的设备上该应用程序
它的正常工作。但是,当我使用的是移动网络或3G它给了一个错误。
它不工作在移动网络上。

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.

我用这code:

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

和我ConnectionTools类code:

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?

我们应该为移动网络或3G呢?

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

推荐答案

试试这个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;
}

您updateConnectionSuffix替换该功能()。

Replace this function by your updateConnectionSuffix().

这篇关于的HttpConnection与移动网络或3G连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆