我正在尝试从我的网络服务下载并显示图像但我收到错误:
net.rim.device.cldc.io.ssl.TLSIOException(net.rim.device.cldc.io.ssl.TLSException(net.rim.device.api.io.ConnectionClosedException:Not connected))
这是我获取图片的代码:
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调用它; bitmap =(getBitmapFromUrl(strsessionPictureUrl));
错误表示连接已关闭。我想知道为什么会这样。我的代码中是否有一些小故障?
答案 0 :(得分:2)
您好我认为这是连接扩展问题,因此请尝试将以下两种方法添加到您的Utility类或同一类中,并调用此方法以获取合适的扩展名
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");
}
}