Android 如何知道改装呼叫何时完成
有没有办法知道我的改装电话什么时候完成了它的任务?我想知道何时收到所有数据,以便代码可以继续,比如启动另一个活动或使用第一次调用的数据进行第二次调用 注:我正在使用一个异步请求(.enqueue) 编辑:Android 如何知道改装呼叫何时完成,android,web-services,api,retrofit,retrofit2,Android,Web Services,Api,Retrofit,Retrofit2,有没有办法知道我的改装电话什么时候完成了它的任务?我想知道何时收到所有数据,以便代码可以继续,比如启动另一个活动或使用第一次调用的数据进行第二次调用 注:我正在使用一个异步请求(.enqueue) 编辑: getContact(){ //Get a contact List from the server Call<List<ModelContact>> callM = contactInterface.createRContact(listConta
getContact(){
//Get a contact List from the server
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
// Fetch the List from Response and save it on the local database
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
// Fetch the Configs object and save it on the database
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
//Here I need to start a new activity after the calls
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
getContact(){
//从服务器获取联系人列表
调用callM=contactInterface.createRContact(listContact);
callM.enqueue(新回调(){
@凌驾
公共响应(响应、改装){
//从响应中获取列表并将其保存在本地数据库中
}
@凌驾
失效时的公共无效(可丢弃的t){
Log.i(“标记”,“错误:+t.getMessage());
}
});
//获取包含某些配置的对象
调用callC=contactInterface.getConfigs(Configs);
enqueue(新的回调(){
@凌驾
公共响应(响应、改装){
//获取Configs对象并将其保存在数据库中
}
@凌驾
失效时的公共无效(可丢弃的t){
Log.i(“标记”,“错误:+t.getMessage());
}
});
//在这里,我需要在通话后开始新的活动
Intent loginact=新的Intent(TokenActivity.this、LoginActivity.class);
星触觉(loginact);
}
也许您可以使用两个布尔标志并在getContact方法之外启动新的意图
大概是这样的:
public class MyActivity extends Activity {
//lot of code omitted
private boolean cIsFinished;
private boolean mIsFinished;
private void getContact(){
//create M and C
m.onResponse(){
//do whatever you want with data and call startIntent
mIsFinished=true;
startIntent();
}
c.onResponse(){
//do whatever you want with data and call startIntent
cIsFinished=true;
startIntent();
}
}
private synchronized void startIntent(){
if(cIsFinished && mIsFinished){
//startIntentHere!
Intent intent = new blah blah blah
}
}
}
移动
如果您有任何相互依赖的请求,它将是复杂的。通过使用flatMap方法链接多个请求并避免回调地狱,可以使用RxJava实现这一点。这很简单。你可以在这里学习
您可以向enqueue(又名listener)提供一个委托。我在哪里可以找到代码示例,我已经看了它,但什么也没找到……为什么不发布您的尝试?发布了,感谢您的回答您想在启动新活动之前等待两个调用返回吗?在代码到达StartEnt()时使用此方法还没有收到响应,就到此为止,我需要它有响应,比如当变量设置为true时,它会再次进行测试。。。有什么办法吗?我试着不要进入一个回调地狱,因为以后我要用8个不同的调用来做同样的事情,而将一个调用设置在另一个调用中会有点混乱。。。我想参考链接断了
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
// Fetch the Configs object and save it on the database
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
getContact(){
//Get a contact List from the server
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
// Fetch the List from Response and save it on the local database
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
//Fetch the Configs object and save it on the database
//Here I need to start a new activity after the calls
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
}