我有一个简单的片段来处理我的应用程序的登录。由于我正在处理登录请求,我不想缓存它们。此策略正常工作,直到我在请求中间引入暂停或方向更改。当用户单击登录按钮时,我显示ProgressDialog。当我得到响应(成功或失败)时,这就消失了。如果我进入主屏幕,然后在登录请求中间返回应用程序,我的监听器永远不会得到通知,因此我的ProgressDialog不会被解雇,我的应用程序被冻结。我尝试在我的onStart中添加spiceManager.getFromCache。这有帮助,但是当应用程序尝试恢复时结果始终为null ...这是有道理的,因为结果不会被缓存。配置我的侦听器以在此方案中得到通知的正确方法是什么?
// using Jackson2SpringAndroidSpiceService
public void onStart() {
super.onStart();
spiceManager.start(getActivity());
spiceManager.addListenerIfPending(AccessTokenResponse.class, null,
new AccessTokenResponseRequestListener());
//spiceManager.getFromCache(AccessTokenResponse.class,
// null, DurationInMillis.ALWAYS_EXPIRED,
// new AccessTokenResponseRequestListener());
}
private void performRequest(String username, String password) {
progressDialog = ProgressDialog.show(getActivity(), "", "Logging in...", true);
LoginFragment.this.getActivity().setProgressBarIndeterminateVisibility(true);
LoginRequest request = new LoginRequest(username, password);
spiceManager.execute(request, null, DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());
}
private class AccessTokenResponseRequestListener implements RequestListener<AccessTokenResponse> {
@Override
public void onRequestFailure(SpiceException e) {
//update your UI
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
buttonLogin.setEnabled(true);
Log.e(TAG, "Login unsuccessful");
if(e.getCause() instanceof HttpClientErrorException)
{
HttpClientErrorException exception = (HttpClientErrorException)e.getCause();
if(exception.getStatusCode().equals(HttpStatus.BAD_REQUEST))
{
Log.e(TAG, "Login unsuccessful");
Toast.makeText(getActivity().getApplicationContext(),
"Wrong username/password combo!",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getActivity().getApplicationContext(),
"Login unsuccessful! If the problem persists, please contact support.",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Login unsuccessful! If the problem persists, please contact support.",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestSuccess(AccessTokenResponse accessToken) {
//update UI
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
buttonLogin.setEnabled(true);
if (accessToken != null) {
OnAuthenticatedListener listener = (OnAuthenticatedListener) getActivity();
listener.userLoggedIn(editTextUsername.getText().toString(), accessToken);
}
}
}
答案 0 :(得分:3)
使用缓存。使用一些缓存键执行请求
spiceManager.execute(request, "your_cache_key", DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());
并且在侦听器中,如果在切换到另一个活动之前成功返回,则会从缓存中删除对此请求的响应,因为您不希望根据您的要求缓存帐户信息。
@Override
public void onRequestFailure(SpiceException e) {
....
spiceManager.removeDataFromCache(AccessTokenResponse.class);
....
}
@Override
public void onRequestSuccess(AccessTokenResponse accessToken) {
if (accessToken == null) {
return;
}
....
spiceManager.removeDataFromCache(AccessTokenResponse.class);
....
}
在onStart尝试获取缓存响应,如果您切换到另一个活动,现在回到上一个活动。这个返回响应是在调用spiceManager.shouldStop()之后到达的。否则返回null。
spiceManager.getFromCache(AccessTokenResponse.class, "your_cache_key", DurationInMillis.ALWAYS_RETURNED, new AccessTokenResponseRequestListener());