等到 Refrotfit return 成功或失败
Wait until Refrotfit return success or failure
我有登录按钮,当用户点击它时,我会调用 Api-服务(我使用改造)来检查 he/she 是否已注册,类似这样
private void loginUserService() {
ServiceHelper.getInstance().loginUser(usernameEdtTxt.getText().toString(), passwordEdtTxt.getText().toString(), new Callback<String>() {
@Override
public void success(String s, Response response) {
if (!TextUtils.isEmpty(s)) {
isLoginSuccessfull = true;
}
}
@Override
public void failure(RetrofitError retrofitError) {
isLoginSuccessfull = false;
}
});
}
我怎样才能等到这个过程完成并 return 正确的值? (虽然此调用是异步的)
我最后一次尝试:我将此方法放入 AsynTask 并从 OnPostExecute 中获取 return 值,但它似乎无法正常工作!
实现此目的的最简单且也许是最好的方法是使用事件。以下是如何使用名为 EventBus.
的库进行尝试
使用 gradle,将以下行添加到依赖项部分:
compile 'de.greenrobot:eventbus:2.4.0'
现在,在 activity 的 onCreate 方法或片段中注册 EventBus,以便在 Retrofit 请求成功时收到通知或失败。您使用这样的行注册 EventBus:
EventBus.getDefault().register(this);
创建一个简单的 POJO(普通旧 Java 对象),您可以将其命名为 RetrofitEvent 并添加一个变量,例如:
public boolean isRetrofitCompleted;
然后在这个 class 的构造函数中初始化这个变量。如果需要,您可以添加 setter 方法,但这不是必需的。
现在,在 Retrofit onSuccess() 方法中,您可以使用以下行通知 activity 或片段事件已成功完成:
EventBus.getDefault().post(new RetrofitEvent(true));
或者如果失败:
EventBus.getDefault().post(new RetrofitEvent(false));
现在回到您的 activity class 或片段并通过覆盖 onEvent 方法来监听此事件,如下所示:
public void onEvent(RetrofitEvent event) {
if(event.isRetrofitCompleted){
//if you had a progress dialog showing, hide it here.
//then of course do what you needed here.
}else{
//the request might have failed here due to network issues
//update the ui accordingly.
}
}
- 最后,记得像这样在 onDestroy 方法中注销 EventBus:
EventBus.getDefault().unregister(this);
我有登录按钮,当用户点击它时,我会调用 Api-服务(我使用改造)来检查 he/she 是否已注册,类似这样
private void loginUserService() {
ServiceHelper.getInstance().loginUser(usernameEdtTxt.getText().toString(), passwordEdtTxt.getText().toString(), new Callback<String>() {
@Override
public void success(String s, Response response) {
if (!TextUtils.isEmpty(s)) {
isLoginSuccessfull = true;
}
}
@Override
public void failure(RetrofitError retrofitError) {
isLoginSuccessfull = false;
}
});
}
我怎样才能等到这个过程完成并 return 正确的值? (虽然此调用是异步的)
我最后一次尝试:我将此方法放入 AsynTask 并从 OnPostExecute 中获取 return 值,但它似乎无法正常工作!
实现此目的的最简单且也许是最好的方法是使用事件。以下是如何使用名为 EventBus.
的库进行尝试使用 gradle,将以下行添加到依赖项部分:
compile 'de.greenrobot:eventbus:2.4.0'
现在,在 activity 的 onCreate 方法或片段中注册 EventBus,以便在 Retrofit 请求成功时收到通知或失败。您使用这样的行注册 EventBus:
EventBus.getDefault().register(this);
创建一个简单的 POJO(普通旧 Java 对象),您可以将其命名为 RetrofitEvent 并添加一个变量,例如:
public boolean isRetrofitCompleted;
然后在这个 class 的构造函数中初始化这个变量。如果需要,您可以添加 setter 方法,但这不是必需的。
现在,在 Retrofit onSuccess() 方法中,您可以使用以下行通知 activity 或片段事件已成功完成:
EventBus.getDefault().post(new RetrofitEvent(true));
或者如果失败:
EventBus.getDefault().post(new RetrofitEvent(false));
现在回到您的 activity class 或片段并通过覆盖 onEvent 方法来监听此事件,如下所示:
public void onEvent(RetrofitEvent event) { if(event.isRetrofitCompleted){ //if you had a progress dialog showing, hide it here. //then of course do what you needed here. }else{ //the request might have failed here due to network issues //update the ui accordingly. } }
- 最后,记得像这样在 onDestroy 方法中注销 EventBus:
EventBus.getDefault().unregister(this);