在我的应用中,我需要从服务器下载多个图像。我使用这段代码来获取一个字节数组:
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的后缀为“; deviceSide = false; ConnectionType = MDS - public”(不含空格),并且运行良好。
问题是,对于没有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;
连接有效,因为它选择了好的前缀(在我们的例子中是interface = wifi),但这会产生另一个问题。
有些图片没有很好地下载,其中一些(每次尝试都不一样)都已损坏,但只有当手机使用wifi连接才能获取这些图片时。
我该如何避免这个问题?我必须使用什么方法来获得连接?是否可以检查用户是否拥有使用MDS的SIM卡 - 公共?
以下是损坏图片的示例:
error image http://nsa30.casimages.com/img/2012/06/28/120628033716123822.png
答案 0 :(得分: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;
}
答案 1 :(得分:0)
您可以查看BIS_B(MDS公开)的覆盖范围是否足够,但如果您尝试支持无SIM用户,则无法帮助您。我想知道问题是否在Wi-Fi连接和IOUtilities.streamToBytes()之间无法比较。请按照API documents中的建议尝试编码。
答案 2 :(得分:0)
当你追加interface = wifi时会发生什么?您是否可以运行附加到kb以下文章的网络诊断工具并运行所有已删除SIM的测试
另请注意,通过BES / MDS下载大型文件时,MDS会施加限制。请确保您查看下面的kb文章 http://supportforums.blackberry.com/t5/Java-Development/Download-large-files-using-the-BlackBerry-Mobile-Data-System/ta-p/44585