提问人:Awos Al-Radaideh 提问时间:5/27/2023 最后编辑:KozmotronikAwos Al-Radaideh 更新时间:5/27/2023 访问量:35
onPostExcute 在 asynctask 中的 doInbackground 之前执行
onPostExcute executed before doInbackground in asynctask
问:
我在里面调用了 api,我正在使用 okhtpp 然后我将请求排队,实际上 enqueue 不会阻塞主线程,因此直接执行如何解决这个问题asynctask
doInBackGround
asyncTask
onPostExcute()
onPostExcute()
我显示了新视图,但是当执行时,它会在API请求完成之前直接打开新视图
完成 API 请求后显示视图的最佳方式是什么asyncTask
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);
}
}
我在这里打电话给班级
if (Q.isEmpty()){
Toast.makeText(activty.this,"empty",Toast.LENGTH_SHORT).show();
} else{
new startRequest().execute(Q);
}
这是代码示例,而不是完整的代码,因为完整的代码太大了
答: 暂无答案
评论
enqueue()
= 异步,= 同步。execute()
doInBackground extends AsyncTask
??请不要这样命名你的任务,因为如你所知,doInBackground 是异步任务的私有成员。非常令人困惑。以这种方式无法调侃的代码。