0

在我的应用程序中,我需要从服务器下载多个图像。我使用此代码获取字节数组:

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

网址由后缀“;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 连接来获取这些图像时。

我怎样才能避免这个问题?我必须使用什么方法来获得连接?是否可以检查用户是否有 SIM 卡才能使用 MDS-public?

这是损坏图像的示例:

错误图片 http://nsa30.casimages.com/img/2012/06/28/120628033716123822.png

4

3 回答 3

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;
}
于 2012-07-09T12:19:07.387 回答
0

您可以检查 BIS_B(MDS 公共)的覆盖范围是否足够,但如果您尝试支持无 SIM 卡的用户,这对您没有帮助。我想知道问题是否在于 Wi-Fi 上的连接和 IOUtilities.streamToBytes() 之间的无法比较。尝试按照API 文档中的建议进行编码。

于 2012-06-28T15:21:42.107 回答
0

当您附加 interface=wifi 时会发生什么?您能否运行以下 kb 文章所附的网络诊断工具并在移除 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

于 2012-07-05T23:21:20.310 回答