我不完全确定如何正确使用ConnectionFactory,但这是我的ImageThread示例,每次有图像时调用ConnectionFactory,在任何给定的屏幕上都有一堆。
public class ImageThread extends Thread {
private String url;
private HttpConnection httpConn;
private InputStream is;
private JSONArray array;
private Bitmap image;
private ImageThreadCallback c;
private static boolean hasImageCache = false;
private static MultiMap imageCache;
public ImageThread(String url, ImageThreadCallback c, String ident){
System.out.println("Connection begin!");
this.url = url;
this.c = c;
}
public void notifyUs(){
this.c.update(image);
}
public void run(){
myConnectionFactory connFact = new myConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
System.out.println("Connection factory!");
if(connDesc != null)
{
System.out.println("Connection not null!");
httpConn = (HttpConnection) connDesc.getConnection();
try {
httpConn.setRequestMethod(HttpConnection.GET);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
is = null;
try
{
final int iResponseCode = httpConn.getResponseCode();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
System.out.println("Connection in run!");
// Get InputConnection and read the server's response
InputConnection inputConn = (InputConnection) httpConn;
try {
is = inputConn.openInputStream();
System.out.println("Connection got inputstream!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] data = null;
try {
data = IOUtilities.streamToBytes(is);
System.out.println("Connection got data!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EncodedImage hai = EncodedImage.createEncodedImage(data, 0, data.length);
image = hai.getBitmap();
notifyUs();
}
});
}
catch(IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
}
}
}
我知道这个例子与示例相同,但是应该为ImageThread的每个实例调用ConnectionFactory吗?我问这个问题是因为我的应用程序在使用它时会突然失去连接。输入/输出图标不再闪烁。我在想它可能是对ConnectionFactory的误用?
有什么想法吗?
答案 0 :(得分:1)
不,除非您想要更改连接首选项(如连接模式或超时),否则无需创建新实例。
连接丢失对我来说似乎是一个不同的问题。重用收集意味着您将节省堆内存,但我认为它与其他内容无关。您可以尝试在设备上进行调试,并查看您获得的HTTP错误代码。
答案 1 :(得分:0)
问题是您正在对应用程序的事件线程执行I / O操作。 当您使用invokeLater(Runnable r)时,r将在Application的事件线程上执行。很可能,Blackberry运行时会终止该应用程序。
就我看到你的代码而言,你不必创建传递给invokeLater()的匿名内部类Runnable。你可以这样做:
if(connDesc != null)
{
System.out.println("Connection not null!");
httpConn = (HttpConnection) connDesc.getConnection();
try {
httpConn.setRequestMethod(HttpConnection.GET);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
is = null;
try
{
final int iResponseCode = httpConn.getResponseCode();
System.out.println("Connection in run!");
// Get InputConnection and read the server's response
InputConnection inputConn = (InputConnection) httpConn;
try {
is = inputConn.openInputStream();
System.out.println("Connection got inputstream!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] data = null;
try {
data = IOUtilities.streamToBytes(is);
System.out.println("Connection got data!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EncodedImage hai = EncodedImage.createEncodedImage(data, 0, data.length);
image = hai.getBitmap();
notifyUs();
catch(IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
尽管如此,我发现代码存在其他问题,如果你做出我提到的修正,那也没关系。