2012-01-17 36 views
1

我不完全確定如何正確使用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的濫用?

任何想法?

回答

1

不,您不需要創建新實例,除非您想更改連接首選項,例如連接模式或超時。

連通性的喪失對我來說似乎是一個不同的問題。重新使用收集意味着您將保存堆內存,但我認爲它與其他事情無關。你可以嘗試在設備上進行調試,看看你得到了什麼HTTP錯誤代碼。

0

問題是您正在應用程序的事件線程上執行I/O操作。 當您使用invokeLater(Runnable r)時,r會在Application的事件線程上執行。黑莓運行庫很有可能會殺死應用程序。就我看到你的代碼而言,你不需要創建傳遞給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()); 
     } 

雖然,我發現代碼的其他問題,如果你進行我提到的更正會是好的。