onPostExcute executed before doInbackground in asynctask

42 Views Asked by At

i have asynctask which i call api inside doInBackGround and i'm using okhtpp and then i enqueue the request actually the enqueue does not block the main thread so the asyncTask execute onPostExcute() directly how to fix this

onPostExcute() i show new view but when excute asyncTask it opens new view directly before the api request is done what is the best way to show the view after api request is done

public class makeTheRequest extends AsyncTask<String ,String,Void> {
    @Override
    protected Void doInBackground(String... strings) {
        Request request = new Request.Builder()
                .url("https://api")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                //show failed
            }


            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                //show response
            }
        }
    }
    
    @Override
    protected void onPostExecute(Void unused) {
        super.onPostExecute(unused);
        rootView.addView(alternateLayout);
    }
}

and im calling the class here

if (Q.isEmpty()){
                    
    Toast.makeText(activty.this,"empty",Toast.LENGTH_SHORT).show();

} else{
    new startRequest().execute(Q);
}

this is code sample not the full code did this cuz the full code is too large

0

There are 0 best solutions below