2016-07-26 85 views
0

当我调试我的应用程序时,我看到onPostExecute在onPreExecute后启动,并且只有在完成时doInBackground方法才开始运行,所以我没有UI上的结果。为什么会这样?的AsyncTask代码:为什么onPostExecute可以在AsyncTask的doInBackground之前运行?

class TranslateYandex extends AsyncTask<Void, Void, Void> { 
    String translate = ""; 
    //  YandexTranslation yandexTranslation; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     enterWord.setEnabled(false); 
     getTranslateButton.setEnabled(false); 
     translate = enterWord.getText().toString(); 
    } 
    @Override 
    protected Void doInBackground(Void... voids) { 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("https://translate.yandex.net") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
     YandexService service = retrofit.create(YandexService.class); 

     Call<YandexTranslation> call = service.getTranslation(translate, API_KEY, LANG); 
     call.enqueue(new Callback<YandexTranslation>() { 
      @Override 
      public void onResponse(Call<YandexTranslation> call, Response<YandexTranslation> response) { 
       if (response.body() != null){ 
        Log.i("Response", response.body().getTranslation().get(0)); 
        translation = response.body().getTranslation().get(0); 
        int donothing = 1; 
       } 

       else { 
        Log.i("Response", " is null"); 
        } 
      } 

      @Override 
      public void onFailure(Call<YandexTranslation> call, Throwable t) { 
       Log.i("Failure", t.toString()); 
      } 
     }); 

     return null; 
    } 

    protected void onPostExecute(Void voids) { 
     enterWord.setEnabled(true); 
     getTranslateButton.setEnabled(true); 
     enterTranslation.setText(translation); 
    } 
} 
+3

因为你在'doInBackground()'中做的事情本身就是异步的。也就是说,你不需要把它们放在'AsyncTask'中。 –

+0

您没有在'onPostExecute'上放置'@ Override'注释 –

+0

@KevinMurvie Java中不需要'@ Override'注解。签名对于给定的类型参数是正确的。 –

回答

2

我觉得没有必要使用异步任务简单地创建像下面我已经证明了你会实现你想要的东西的方法。

public void methodName(){ 

    enterWord.setEnabled(false); 
    getTranslateButton.setEnabled(false); 
    translate = enterWord.getText().toString(); 

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("https://translate.yandex.net") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    YandexService service = retrofit.create(YandexService.class); 

    Call<YandexTranslation> call = service.getTranslation(translate, API_KEY, LANG); 
    call.enqueue(new Callback<YandexTranslation>() { 
     @Override 
     public void onResponse(Call<YandexTranslation> call, Response<YandexTranslation> response) { 
      if (response.body() != null){ 
       Log.i("Response", response.body().getTranslation().get(0)); 
       translation = response.body().getTranslation().get(0); 
       int donothing = 1; 
      } 

      else { 
       Log.i("Response", " is null"); 
       } 
     } 

     @Override 
     public void onFailure(Call<YandexTranslation> call, Throwable t) { 
      Log.i("Failure", t.toString()); 
     } 


     /* Here i am adding this code global because it seems you do not have any specific condition for translation object in onResponse. You can also write this in onResponse with specific condition*/ 
     enterWord.setEnabled(true); 
     getTranslateButton.setEnabled(true); 
     enterTranslation.setText(translation); 
    }); 
} 

现在只需从您想要的位置调用此函数。

让我知道你是否面临此解决方案的任何问题。

如果您可以通过此解决方案解决您的查询,请将此标记为答案。

快乐编码!

+0

谢谢,它工作,但我已经将UI更新(最后3行)移动到call.enqueue的onResponse方法,因为它是异步的,所以我需要在运行时禁用按钮并在完成后更新。 –

+0

欢迎!如果您发现它有帮助,请将其标记为答案,以便对面临同类问题的其他开发人员有所帮助。 –

相关问题