gpt4 book ai didi

java - 如何使用 RoboSpice 从中断的未缓存请求中正确恢复

转载 作者:太空狗 更新时间:2023-10-29 12:40:25 25 4
gpt4 key购买 nike

我有一个简单的 fragment 来处理我的应用程序的登录。因为我正在处理登录请求,所以我不想缓存它们。在我在请求中间引入暂停或方向更改之前,此策略工作正常。当用户单击登录按钮时,我会显示一个 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);
}

}
}

最佳答案

使用缓存。使用一些缓存键执行请求

spiceManager.execute(request, "your_cache_key", DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());

如果在您切换到另一个 Activity 之前成功返回,则在监听器中从缓存中删除对此请求的响应,因为您不想根据您的要求缓存帐户信息。

@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 中,如果您切换到另一个 Activity 并现在返回到上一个 Activity ,则尝试获取缓存的响应。此返回响应在您调用 spiceManager.shouldStop() 后到达。否则返回 null。

spiceManager.getFromCache(AccessTokenResponse.class, "your_cache_key", DurationInMillis.ALWAYS_RETURNED, new AccessTokenResponseRequestListener());

关于java - 如何使用 RoboSpice 从中断的未缓存请求中正确恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27092185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com