2012-06-28 53 views
0

在我的應用程序中,我需要從服務器下載多個圖像。我用這個代碼來獲得一個字節數組:黑莓 - 下載的圖像在HttpConnection無線上受到破壞

HttpConnection connection = null; 
InputStream inputStream = null; 
byte[] data = null; 

try 
{ 
//connection = (HttpConnection)Connector.open(url); 
connection = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true); 

     int responseCode = connection.getResponseCode();    
     if(responseCode == HttpConnection.HTTP_OK) 
     { 
      inputStream = connection.openInputStream(); 
      data = IOUtilities.streamToBytes(inputStream); 
      inputStream.close(); 
     }   
     connection.close(); 

     return data; 
    } 
    catch(IOException e) 
    { 
     return null; 
    } 

的URL與後綴形成「裝置側= FALSE; ConnectionType = MDS - 大衆」(無空格),它工作得很好。

問題是,對於沒有SIM卡的手機,我們無法通過MDS服務器連接到互聯網。所以我們改爲使用連接工廠和讓BB選擇任何他想做的:

ConnectionFactory connFact = new ConnectionFactory(); 
    ConnectionDescriptor connDesc; 
    connDesc = connFact.getConnection(url); 

    if (connDesc != null) 
    { 
     final HttpConnection httpConn; 
     httpConn = (HttpConnection)connDesc.getConnection(); 
     try 
     { 
      httpConn.setRequestMethod(HttpConnection.GET); 
      final int iResponseCode = httpConn.getResponseCode(); 
      if(iResponseCode == HttpConnection.HTTP_OK) 
      { 
       InputStream inputStream = null; 
       try{ 
        inputStream = httpConn.openInputStream(); 
        byte[] data = IOUtilities.streamToBytes(inputStream); 
        return data; 
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
        return null; 
       } 
       finally{ 
        try 
        { 
         inputStream.close(); 
        } catch (IOException e) 
        { 
         e.printStackTrace(); 
         return null; 
        } 
       } 
      } 
     } 
     catch (IOException e) 
     { 
      System.err.println("Caught IOException: " + e.getMessage()); 
     } 
    } 
    return null; 

的連接工作,因爲它選擇好前綴(接口=在我們的例子WIFI),但這個創造了另一個問題。

某些圖像沒有很好地下載,其中一些(每次嘗試都不相同)都被破壞,但只有當手機使用wifi連接才能獲取這些圖像時。

我該如何避免這個問題?我必須使用什麼方法才能獲得連接?是否有可能檢查用戶是否有SIM卡以便使用MDS - 公共?

這裏是一個損壞的圖像的一個例子:

error image http://nsa30.casimages.com/img/2012/06/28/120628033716123822.png

回答

0

當你追加interface = wifi時會發生什麼?您可以運行連接到下面的知識庫文章,網絡診斷工具,並運行SIM所有測試去除

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-OS/ta-p/614822

也請注意,當下載大文件在BES/MDS有通過MDS強加的限制。請確保您查看下面的知識庫文章 http://supportforums.blackberry.com/t5/Java-Development/Download-large-files-using-the-BlackBerry-Mobile-Data-System/ta-p/44585

0

您可以檢查,看看是否覆蓋足夠BIS_B(MDS公共),但不會幫助你,如果你想支持無SIM的用戶。我想知道這個問題是否與Wi-Fi和IOUtilities.streamToBytes()之間的連接不兼容。按照API documents中的建議嘗試編碼。

+0

我試着不使用RIM類IOUtilities和使用基本的循環,但是當我使用WiFi無我仍然遇到這個問題一張SIM卡。 –

1

試試這個:

public static String buildURL(String url) { 
    String connParams = ""; 


     if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 
      connParams = ";interface=wifi"; //Connected to a WiFi access point. 
     } else { 
      int coverageStatus = CoverageInfo.getCoverageStatus(); 
      // 
      if ((coverageStatus & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B) { 
       connParams = ";deviceside=false;ConnectionType=mds-public"; 
      } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 
       // Have network coverage and a WAP 2.0 service book record 
       ServiceRecord record = getWAP2ServiceRecord(); 
       // 
       if (record != null) { 
        connParams = ";deviceside=true;ConnectionUID=" + record.getUid(); 

       } else { 
        connParams = ";deviceside=true"; 
       } 
      } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { 
       // Have an MDS service book and network coverage 
       connParams = ";deviceside=false"; 
      } 
     } 

     Log.d("connection param"+url+connParams); 

    // 
    return url+connParams; 
} 

private static ServiceRecord getWAP2ServiceRecord() { 
    String cid; 
    String uid; 
    ServiceBook sb = ServiceBook.getSB(); 
    ServiceRecord[] records = sb.getRecords(); 
    // 
    for (int i = records.length -1; i >= 0; i--) { 
     cid = records[i].getCid().toLowerCase(); 
     uid = records[i].getUid().toLowerCase(); 
     // 
     if (cid.indexOf("wptcp") != -1 
       && records[i].getUid().toLowerCase().indexOf("wap2") !=-1 
       && uid.indexOf("wifi") == -1 
       && uid.indexOf("mms") == -1) { 
      return records[i]; 
     } 
    } 
    // 
    return null; 
}