在我的應用程序中,我需要從服務器下載多個圖像。我用這個代碼來獲得一個字節數組:黑莓 - 下載的圖像在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
我試着不使用RIM類IOUtilities和使用基本的循環,但是當我使用WiFi無我仍然遇到這個問題一張SIM卡。 –