Image BlackBerry-下载的图像在带有HttpConnection的wifi上损坏

Image BlackBerry-下载的图像在带有HttpConnection的wifi上损坏,image,blackberry,httpconnection,corrupt,Image,Blackberry,Httpconnection,Corrupt,在我的应用程序中,我需要从服务器上下载几个图像。我使用以下代码获取字节数组: HttpConnection connection = null; InputStream inputStream = null; byte[] data = null; try { //connection = (HttpConnection)Connector.open(url); connection = (HttpConnection)Connector.open(url, Connector.READ_

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

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;
连接之所以有效,是因为它选择了好的前缀(在我们的例子中,接口=wifi),但这会产生另一个问题

有些图像下载不好,有些图像(不是每次尝试时的图像)已损坏,但只有在手机使用wifi连接获取这些图像时才会损坏

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

以下是损坏图像的示例:


您可以检查覆盖范围是否足以满足BIS_B(MDS public)的要求,但如果您试图支持无SIM卡用户,这将无助于您。我想知道问题是否出在Wi-Fi连接和IOUtilities.streamToBytes()之间的不兼容上。尝试按照中的建议进行编码。

附加interface=wifi时会发生什么?您是否可以运行以下知识库文章附带的网络诊断工具,并在SIM卡已删除的情况下运行所有测试

还请注意,当通过BES/MDS下载大型文件时,MDS会施加一些限制。请确保您阅读了以下知识库文章 试试这个:

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

我试着不使用RIM类IOUtilities和基本环路,但当我在没有SIM卡的情况下使用wifi时,仍然会遇到这个问题。