Question

In my app I need to download several images from a server. I use this code to get a byte array :

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

The url are formed with the suffix ";deviceSide=false;ConnectionType=MDS - public" (without spaces) and it is working perfectly well.

The problem is that with phones that do not have a sim card, we can't connect to the internet via the MDS server. So we changed to use the connection factory and let BB choose whatever he wants :

    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;

The connection works because it select the good prefix (interface=wifi in our case), but this create another problem.

Some images are not well downloaded, some of them (not the sames at each try) are corrupted, but only when the phone use a wifi connection to get these images.

How can I avoid this problem ? What method to get a connection do I have to use ? Is it possible to check if the user have a sim card in orderto use MDS - public ?

Here is an example of a corrupted image :

error image http://nsa30.casimages.com/img/2012/06/28/120628033716123822.png

Was it helpful?

Solution

What happens when you append interface=wifi? Can you run the network diagnostic tool attached to below kb article and run all tests with SIM removed

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-OS/ta-p/614822

Please also note that when download large files over BES/MDS there are limits imposed by MDS. Please ensure you review the below kb article http://supportforums.blackberry.com/t5/Java-Development/Download-large-files-using-the-BlackBerry-Mobile-Data-System/ta-p/44585

OTHER TIPS

try this:

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

You can check to see if coverage is sufficient for BIS_B (MDS public) but that won't help you if you are trying to support SIM-less users. I wonder if the problem is in an incomparability between the connection on Wi-Fi and IOUtilities.streamToBytes(). Try coding as recommended in the API documents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top