Domanda

I am trying to download and show an image from my webservice but I get the error:

net.rim.device.cldc.io.ssl.TLSIOException(net.rim.device.cldc.io.ssl.TLSException(net.rim.device.api.io.ConnectionClosedException: Not connected))

This is my code to get the image:

public Bitmap getBitmapFromUrl(String url)
        {
        Bitmap bitmap=null;
        try
        {
            HttpConnection connection=(HttpConnection)Connector.open(url+ ";deviceside=true;ConnectionUID=S TCP");
            connection.setRequestMethod(HttpConnection.GET);
            InputStream is=connection.openInputStream();
            int length=is.available();
            System.out.println("InputStream length "+length);
            byte[] data=new byte[length];  
            System.out.println("byte data "+data);
            data=IOUtilities.streamToBytes(is); 
            System.out.println("IOUtilities data "+data);
            connection.close();
            is.close();
            bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);
            if(bitmap!=null)
                return bitmap;
         else
                return bitmap=Bitmap.getBitmapResource("me1.jpg");
        }
        catch (Exception e) 
        {
            System.out.println("The image has not been fetched");
            return bitmap=Bitmap.getBitmapResource("me2.jpg");
        }
        }

I am calling it with Bitmap bitmap=null; bitmap = (getBitmapFromUrl(strsessionPictureUrl));

The error says connection closed. I wonder why is this happening. Is there some glitch in my code?

È stato utile?

Soluzione

Hi i think it is connection extension issue so try to add following two methods to your Utility class or in same class and call this method for getting suitable extension

    public static String getConnectionString()
    {            
        String connectionString = null;

        // Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
        if(DeviceInfo.isSimulator())
        {                   
                        logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
                        connectionString = ";deviceside=true";                  
        }    
        // Wifi is the preferred transmission method
        else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
        {
            logMessage("Device is connected via Wifi.");
            connectionString = ";interface=wifi";
        }    
        // Is the carrier network the only way to connect?
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
            logMessage("Carrier coverage.");

            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null)
            {
                // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
                logMessage("No Uid");
                connectionString = ";deviceside=true";
            }
            else
            {
                // otherwise, use the Uid to construct a valid carrier BIBS request
                logMessage("uid is: " + carrierUid);
                connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
            }
        }    
        // Check for an MDS connection instead (BlackBerry Enterprise Server)
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
            logMessage("MDS coverage found");
            connectionString = ";deviceside=false";
        }    
        // If there is no connection available abort to avoid bugging the user unnecssarily.
        else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
        {
            logMessage("There is no available connection.");
        }    
        // In theory, all bases are covered so this shouldn't be reachable.
        else
        {
            logMessage("no other options found, assuming device.");
            connectionString = ";deviceside=true";
        }

        return connectionString;
    }

    /**
     * Looks through the phone's service book for a carrier provided BIBS network
     * @return The uid used to connect to that network.
     */
    public static String getCarrierBIBSUid()
    {
        ServiceRecord[] records = ServiceBook.getSB().getRecords();
        int currentRecord;

        for(currentRecord = 0; currentRecord < records.length; currentRecord++) 
        {
            if(records[currentRecord].getCid().toLowerCase().equals("ippp")) 
            {
               if(records[currentRecord].getName().toLowerCase().indexOf("bibs") >= 0)
                {
                    return records[currentRecord].getUid();
                }
            }
        }

        return null;
    }

Add call that function once and return string take as static

public static String C0NNECTION_EXTENSION="";
public Bitmap getBitmapFromUrl(String url)
{
    if(C0NNECTION_EXTENSION.equals("") )
    {
        C0NNECTION_EXTENSION= getConnectionString();
    }
    Bitmap bitmap=null;
    try
    {
         HttpConnection connection=(HttpConnection)Connector.open(url+C0NNECTION_EXTENSION);
         connection.setRequestMethod(HttpConnection.GET);
         InputStream is=connection.openInputStream();
         int length=is.available();
         System.out.println("InputStream length "+length);
         byte[] data=new byte[length];  
         System.out.println("byte data "+data);
         data=IOUtilities.streamToBytes(is); 
         System.out.println("IOUtilities data "+data);
         connection.close();
         is.close();
         bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);
         if(bitmap!=null)
             return bitmap;
         else
             return bitmap=Bitmap.getBitmapResource("me1.jpg");
     }
     catch (Exception e) 
     {
         System.out.println("The image has not been fetched");
         return bitmap=Bitmap.getBitmapResource("me2.jpg");
     }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top