2015-08-27 50 views
-3

我想在AsyncTaskdoInBackground()塊中發起服務器請求調用。如何在AsyncTask doInBackground()中返回異步操作的結果?

問題是我的服務器調用是由使用回調的異步函數執行的。

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 

    protected Long doInBackground(URL... urls) { 

     ServerLoader.getSharedInstance().get(url, 
      new new AsyncCallback<GetResult<Long>>() { 

      @Override 
      public void onFailure(Exception e) { 
      } 

      @Override 
      public void onSuccess(GetResult<Long> result) { 
       Long myResult = result.getValue(); 
      } 

     ); 

     // return ??; How do I make the return? 
    } 

} 

如何從異步函數調用中獲取返回值?

+0

不要我們e asynctask,那麼,你的電話已經在後臺線程中播放了。 – njzk2

+0

我需要它在後臺線程上完成並在調用後更新UI。 – confile

+0

是的。這就是你的回調。 – njzk2

回答

0

onPostExecute是AsyncTask將代碼張貼到UI的位置,如果這是您要求的。

new DownloadImageTask().execute("http://example.com/image.png"); 


private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
protected Bitmap doInBackground(String... urls) { 
    return loadImageFromNetwork(urls[0]); 
} 

protected void onPostExecute(Bitmap result) { 
    // set image for ImageView 
    mImageView.setImageBitmap(result); 
} 

}

+0

這是屬於我的問題嗎? – confile

3

看看這個模式,它可以幫助我瞭解的AsyncTask enter image description here

+0

我希望有人給我看,當我第一次學習asynctask – Sree

+0

這是真的,但是這並不能回答我的問題,我必須如何同步onInBackground中的異步方法? – confile

+0

我認爲你必須使用get()來同步它。 試試這個classExtendsAsyncTask.execute()。get(); – JFouad

0

由於您的服務器調用以異步方式已經執行,你不需要調用它doInBackground(),只需使用您的服務器通信解決方案提供的設施即可。

如果你想更新結果數據的UI元素,並獲得must be called from main thread,則需要手動發表您的結果處理邏輯與UI AKA主要線程這樣的:

ServerLoader.getSharedInstance().get(url, 
    new new AsyncCallback<GetResult<Long>>() { 

    @Override 
    public void onFailure(Exception e) { 
    } 

    @Override 
    public void onSuccess(GetResult<Long> result) { 
     final Long myResult = result.getValue(); 
     // .post() on a view is what sends stuff to UI thread 
     txtView.post(new Runnable(){ 
      public void run(){ 
       txtView.setText("" + myResult); 
      } 
     }); 
    } 
); 

如果你絕對設置AsyncTask進行代理您的通話ServerLoader,你可以實現一個骯髒的墊片這樣的:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected CountDownLatch mLatch = null; 
    protected Long mResult = null; 
    protected Long doInBackground(URL... urls) { 
     if (urls == null || urls.length = 0) { 
      return null; 
     } 

     mLatch = new CountDownLatch(1); 

     ServerLoader.getSharedInstance().get(url[0], 
      new new AsyncCallback<GetResult<Long>>() { 

      @Override 
      public void onFailure(Exception e) { 
       // leave the result null 
       mLatch.countDown(); 
      } 

      @Override 
      public void onSuccess(GetResult<Long> result) { 
       mResult = result.getValue(); 
       mLatch.countDown(); 
      } 

     ); 

     // the thread that DownloadFilesTask.doInBackground() 
     // gets executed on will wait till it hears from ServerLoader 
     mLatch.await(); 
     return mResult; 
    } 

    protected void onPostExecute(Long result) { 
     // here you get the result 
    } 

} 
相關問題