我有一個處理我的應用程序登錄的簡單片段。由於我正在處理登錄請求,因此我不想緩存它們。此策略正常工作,直到我在請求中引入暫停或方向更改。當用戶點擊登錄按鈕時,我顯示一個ProgressDialog。當我得到迴應(成功或失敗)時,這會消失。如果我進入主屏幕,然後在登錄請求中間返回到應用程序,我的監聽程序將不會收到通知,因此我的ProgressDialog不會被解除,並且我的應用程序被凍結。我嘗試在我的onStart中添加spiceManager.getFromCache。這有幫助,但當應用程序嘗試恢復時,結果始終爲空......這是有道理的,因爲結果沒有被緩存。在這種情況下配置我的偵聽器以通知的正確方法是什麼?如何使用RoboSpice正確地恢復從被中斷的未緩存的請求中恢復
// 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);
}
}
}
下面是我解決它的方法。第一次需要更多的編碼,但是你會得到一個簡單的方法來檢查請求是否已經在運行:http://stackoverflow.com/questions/25681395/robospice-addlistenerifpending-how-do-i -know-IF-傳請求,被發現的/ 32111229#32111229 – Deinlandel 2015-08-20 06:52:27