问题描述
当我调试我的应用程序时,我看到onPostExecute在onPreExecute之后开始,并且仅在doInBackground方法完成后才开始运行,因此我在UI上没有结果.为什么会这样呢? AsyncTask代码:
When I debug my app I see that onPostExecute starts after onPreExecute and only when it is finished doInBackground method start running, so I don't have results on UI. Why it could be? AsyncTask code:
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);
}
}
推荐答案
我认为没有必要使用异步任务,只需创建如下所示的方法即可,我已经证明您将实现您想要的.
i think there is no need to use an async task simply create a method like below i have demonstrated you will achieve what you want.
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);
});
}
现在,只需在所需的位置调用此函数即可.
Now Simply call this function from where you want.
如果您遇到此解决方案的任何问题,请告诉我.
Let me know if you are facing any issue with this solution.
如果您可以使用此解决方案解决查询,请将其标记为答案.
If you can resolve your query with this solution kindly mark this as answer.
快乐编码!
这篇关于为什么onPostExecute可以在AsyncTask中的doInBackground之前运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!