Why could onPostExecute run before doInBackground in AsyncTask?

随声附和 提交于 2020-01-25 13:05:32

问题


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);
    }
}

回答1:


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.

Happy Coding!



来源:https://stackoverflow.com/questions/38585415/why-could-onpostexecute-run-before-doinbackground-in-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!