2012-07-26 93 views
1

当我在使用WiFi 的设备上运行此应用程序时,它工作正常。但是,当我使用移动网络或3G时,它会发生错误。 它不适用于移动网络。HttpConnection连接移动网络或3G

我使用这个代码:

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

而且我ConnectionTools类代码:

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

你能不能给我任何的解决方案?

我们应该为移动网络或3G做些什么?

+0

您是否设置了APN? – Signare 2012-07-26 13:22:45

+0

不,我没有设置APN。应该在哪里设置APN。 – user1350661 2012-07-26 13:33:12

+0

设置它并尝试,你一定会得到 – Signare 2012-07-26 13:41:03

回答

0

让我解释一下: - 这是用于连接使用移动​​网络的2G或3G网络的任何”只是复制粘贴& &享受

字符串URL = “vm.b24esolution.com:9090”;

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

1

试试这个代码。

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()替换这个函数。