0

メソッドがrun()あり、その中でHTTPGETを実行しているAsynctaskを実行しています。run()Asynctaskが結果を取得するのを待ってから続行するにはどうすればよいですか?

4

2 に答える 2

0
//like this
    asyncRequest(URL, parameters, "POST",new RequestListener() {
                @Override
                public void onComplete(String response) { 
                    /*@todo get results and then continue */
                }

                @Override
                public void onIOException(IOException ex) {
                }

                @Override
                public void onFileNotFoundException(FileNotFoundException ex) {
                }

                @Override
                public void onMalformedURLException(MalformedURLException ex) {
                }
            }); 

public static void asyncRequest(final String requestUrl,final Bundle parameters,final String httpMethod,final RequestListener requestListener) {
        new Thread() {
            public void run() {
                try {
                    String resp = MyUtil.openUrl(requestUrl, httpMethod, parameters);
                    requestListener.onComplete(resp);
                } catch (FileNotFoundException ex) {
                    requestListener.onFileNotFoundException(ex);
                            } catch (MalformedURLException ex) {
                                requestListener.onMalformedURLException(ex);
                            } catch (IOException ex) {
                                requestListener.onIOException(ex);
                            }
            }
        }.start();
}

public static interface RequestListener {
    public void onComplete(String response);

    public void onIOException(IOException ex);

    public void onFileNotFoundException(FileNotFoundException ex);

    public void onMalformedURLException(MalformedURLException ex);
}
于 2012-11-14T07:59:10.953 に答える
-1

AsyncTask の onPostExecute メソッドで run 関数を実行する

    private class LongOperation extends AsyncTask<String, Integer, String> {

      @Override
      protected String doInBackground(String... params) {
            //do long asynctask activity here
      }      

      @Override
      protected void onPostExecute(String result) {
           run(); //executes the run method you want here after asysnctask is completed
      }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}   
于 2012-11-14T12:41:41.073 に答える