大家好,我在安卓初始化onCreate方法里写了一个okhttp3的异步get请求方法,可是执行顺序okhttp3的异步里的onResponse总是最后执行,我该怎么让他先执行onResponse里的,代码如下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feeding);
okHttpClient = new OkHttpClient.Builder()
//连接超时
.connectTimeout(5, TimeUnit.SECONDS)
//读取超时
.readTimeout(5, TimeUnit.SECONDS)
//写超时
.writeTimeout(5, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder()
.url("XXXXXXXXXXXX")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
Log.e("Log","我后执行");
}
});
Log.e("Log","先执行了");
}