0

我正在尝试从我的网络服务下载并显示图像,但出现错误:

net.rim.device.cldc.io.ssl.TLSIOException(net.rim.device.cldc.io.ssl.TLSException(net.rim.device.api.io.ConnectionClosedException:未连接))

这是我获取图像的代码:

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

我用 Bitmap bitmap=null; 调用它 位图 = (getBitmapFromUrl(strsessionPictureUrl));

错误说连接已关闭。我想知道为什么会这样。我的代码有问题吗?

4

1 回答 1

2

嗨,我认为这是连接扩展问题,所以尝试将以下两种方法添加到您的实用程序类或同一类中,并调用此方法以获得合适的扩展

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

添加一次调用该函数并返回字符串作为静态

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");
     }
}
于 2012-07-12T11:53:37.247 回答