2011-07-05 55 views
0

假設這是我的NeteorkingMainScreen類,它將顯示從網上搜索到的文本。黑莓中的加載屏幕

public NetworkingMainScreen() { 
     setTitle("Networking"); 
     urlField = new EditField("URL:", ""); 
     textOutputField = new RichTextField(); 
     add(urlField); 
     add(textOutputField); 
    } 
    protected void makeMenu(Menu menu, int instance) { 
     super.makeMenu(menu, instance); 
     menu.add(new MenuItem("Get", 10, 10) { 
     public void run() { 
      getURL(); 
     } 
     }); 
    private void getURL() { 
     HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField.getText(),"GET", this); 
     dispatcher.start(); 
    } 


//********************************************************************************* 
//HttpRequestDispatcher class performs the downloading of contents of webpage. 
public class HttpRequestDispatcher extends Thread { 
private String url; 
private String method; // GET or POST 
private NetworkingMainScreen screen; 
public HttpRequestDispatcher(String url, String method, NetworkingMainScreen screen){ 
    this.url = url; 
    this.method = method; 
    this.screen = screen; 
} 
public void run() { 
try{ 
    HttpConnection connection = (HttpConnection)Connector.open(url); 
    connection.setRequestMethod(method); 
    int responseCode = connection.getResponseCode(); 
     if (responseCode != HttpConnection.HTTP_OK){ 
      screen.requestFailed("Unexpected response code: " + responseCode); 
      connection.close(); 
      return; 
     } 
     String contentType = connection.getHeaderField("Content-type"); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     InputStream responseData = connection.openInputStream(); 
     byte[] buffer = new byte[10000]; 
     int bytesRead = responseData.read(buffer); 

     while(bytesRead > 0) { 
      baos.write(buffer, 0, bytesRead); 
      bytesRead = responseData.read(buffer); 
     } 
     baos.close(); 
     connection.close(); 
     screen.requestSucceeded(baos.toByteArray(), contentType); 
    } 
    catch (IOException ex) { 
     screen.requestFailed(ex.toString()); 
    } 
} 
} 


//*************************************************************************** 
//WaitScreen displays animation till the downloading is completed. 
class WaitScreen extends FullScreen 
{ 
} 

現在我米越來越困惑......

  1. 何時開始等待屏幕類。假設我通過創建一個WaitScreen對象並推動屏幕對象開始。

    protected void makeMenu(Menu menu, int instance) { 
        super.makeMenu(menu, instance); 
        menu.add(new MenuItem("Get", 10, 10) { 
         public void run() 
          UiApplication.getUiApplication.pushScreen(new WaitScreen()); 
          getURL(); 
         } 
        }); 
    

    我的代碼怎麼會知道它應該顯示動畫屏幕和顯示的網頁的內容,即我的意思是我有多麼代碼的人都知道下載數據已經完成。即當我打電話popScreen()? 我將使用I接口如何使用接口以及通過使用接口將獲得哪些幫助。 Plz help

回答

3

這很簡單。

您的HttpRequestDispatcher應該有一個WaitScreen實例的句柄,以便能夠在開始時顯示並在完成時關閉它。

因此,在HttpRequestDispatcher內部,您可以(1)創建WaitScreen。 (2)推動它。 (3)做的東西HttpRequestDispatcher應該做的。 (4)彈出WaitScreen。 Smth like that:

final WaitScreen waitScreen = new WaitScreen(); 

// just to reduce code duplication 
final UiApplication app = UiApplication.getUiApplication(); 

// we are on the non-UI thread, so need 
// to use UiApplication.invokeLater(Runnable action), 
// it safely runs the passed action on the UI thread 
app.invokeLater(new Runnable() { 
    public void run() { 
     app.pushScreen(waitScreen); 
    } 
}); 

try { 
    // main networking actions go here 
} catch (..) { 
    // error handling goes here 
} finally { 
    // make sure we close the waitScreen 
    app.invokeLater(new Runnable() { 
     public void run() { 
      app.popScreen(waitScreen); 
     } 
    }); 
} 
+0

yaa上述信息是有幫助的,但我需要一些關於此的更多指導。所以我再次發佈一些關於此問題的新問題。 –

0

Here,Try this。你所要做的就是把你的代碼放到「運行」功能中。

如果你需要HttpRequest的幫助,或者在課堂上遇到麻煩,請告訴我。我有一個Web類庫,其中設置了線程類來使用該帖子中的類。