2012-09-22 76 views
1

這是我的代碼的HttpConnection不是在真實設備工作-Blackberry

public String Serverconnection(String url) { 

    String line = ""; 

    if (DeviceInfo.isSimulator()) { 
     url = url + ";deviceSide=true"; 
    } 
    try { 
     HttpConnection s = (HttpConnection) Connector.open(url);//*i get the exception here* 
     s.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     s.setRequestProperty(
       "Accept", 
       "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); 
     s.setRequestProperty(HttpHeaders.HEADER_ACCEPT_CHARSET, "UTF-8"); 
     s.setRequestMethod(HttpConnection.GET); 
     InputStream input = s.openInputStream(); 
     byte[] data = new byte[10240]; 
     int len = 0; 
     StringBuffer raw = new StringBuffer(); 

     while (-1 != (len = input.read(data))) { 
      raw.append(new String(data, 0, len)); 
     } 

     line = raw.toString(); 

     input.close(); 
     s.close(); 
    } catch (Exception e) { 
     System.out.println("response--- excep" + line + e.getMessage()); 
    } 
    return line; 

} 

此代碼工作正常,當我在模擬器中運行。但在真實的設備中,我得到了異常「細節不可用 - 虛擬機不支持」「未指定APN。」

我該如何解決這個問題?

+0

你必須添加用於連接互聯網的連接字符串。 – Signare

+0

你通過wifi或gprs連接互聯網? – Signare

+0

我通過wifi連接互聯網 – prakash

回答

4

參閱知識中心文章"Different ways to make an HTTP or socket connection"

追加連接字符串您url.Then嘗試

private static String getConnectionString(){ 
String connectionString=""; 
if(WLANInfo.getWLANState()==WLANInfo.WLAN_STATE_CONNECTED){ 
    connectionString="?;interface=wifi"; 
} 

else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS){ 
    connectionString = "?;&deviceside=false"; 
} 
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT)==CoverageInfo.COVERAGE_DIRECT){ 
    String carrierUid=getCarrierBIBSUid(); 
    if(carrierUid == null) { 
     connectionString = "?;deviceside=true"; 
    } 
    else{ 
     connectionString = "?;deviceside=false?;connectionUID="+carrierUid + "?;ConnectionType=mds-public"; 
    }    
} 
else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {   
     } 
    return connectionString; 
} 

編輯: -

private 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; 
} 
+0

hi Signare我可能知道「String carrierUid = getCarrierBIBSUid();」這個方法做什麼? – prakash

+0

我更新了我的答案。檢查編輯部分。 – Signare

+0

偉大的工作... – prakash

相關問題