2011-07-05 22 views
0

これは、Webから検索されたテキストを表示するNeteorkingMainScreenクラスであるとします。BlackBerryのローディング画面

今私は混乱mは
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ヘルプ

答えて

3

これはやや単純です。

HttpRequestDispatcherにはWaitScreenインスタンスのハンドルがあり、開始時にそれを表示し、完了すると閉じることができます。

したがって、HttpRequestDispatcherの内部では、(1)WaitScreenを作成することができます。 (2)押してください。 (3)HttpRequestDispatcherが行うべきことをしてください。 (4)WaitScreenをポップします。そのようなSmth:

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

上記の情報は役立ちますが、私はこれに関するいくつかのより多くのガイドが必要です。だから私は再びこれに関するいくつかの新しい問題を投稿しています。 –

0

ここでは、thisを試してください。コードを「実行」機能に入れるだけです。

HttpRequestに関するヘルプが必要な場合や、そのクラスに問題がある場合は、お知らせください。私は、そのクラス内のクラスを使用するようにスレッドクラスを設定したWebライブラリを用意しています。

関連する問題