黑莓 - 下载的图像在带有HttpConnection的wifi上损坏



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

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 由后

缀 ";d eviceSide=false;连接类型=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;

连接之所以有效,是因为它选择了良好的前缀(在我们的例子中为 interface=wifi),但这会产生另一个问题。

有些图像下载不好,其中一些(每次尝试都不相同)已损坏,但仅当手机使用 wifi 连接来获取这些图像时。

如何避免此问题?我必须使用什么方法来获取连接?是否可以检查用户是否有SIM卡才能使用MDS - 公共?

下面是损坏的图像的示例:

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

试试这个:

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

当你附加接口=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

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

相关内容

  • 没有找到相关文章

最新更新