我不完全確定如何正確使用ConnectionFactory,但這裏是我的ImageThread示例,每次有圖像時都會調用ConnectionFactory,在任何給定的屏幕上都有一堆圖像。黑莓手機:打開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());
}
}
}
}
我知道這個例子和例子一樣,但是應該調用ConnectionFactory的每個ImageThread實例嗎?我問這個問題是因爲我的應用程序在使用時突然失去連接。輸入/輸出圖標停止閃爍。我在想這可能是對ConnectionFactory的濫用?
任何想法?