我有一个处理我的应用程序登录的简单片段.由于我正在处理登录请求,因此我不想缓存它们.在我在请求中间引入暂停或方向更改之前,该策略可以正常工作.当用户单击登录按钮时,我将显示一个ProgressDialog.当我得到响应(成功或失败)时,这消失了.如果我进入主屏幕,然后在登录请求过程中返回到应用程序,则我的监听器将永远不会收到通知,因此我的ProgressDialog不会被关闭并且我的应用程序将被冻结.我尝试在onStart中添加spiceManager.getFromCache.这很有帮助,但是当应用程序尝试恢复时,结果始终为null …这是有道理的,因为未缓存结果.在这种情况下,配置要通知我的侦听器的正确方法是什么?
// using Jackson2SpringAndroIDSpiceServicepublic 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());
并且在侦听器中,如果您在切换到另一个活动之前成功返回了对该请求的响应,则从缓存中删除对该请求的响应,因为您不想根据需要缓存帐户信息.
@OverrIDepublic voID onRequestFailure(SpiceException e) { .... spiceManager.removeDataFromCache(AccesstokenResponse.class); ....}@OverrIDepublic 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());
总结 以上是内存溢出为你收集整理的java-如何使用RoboSpice从中断的未缓存请求中正确恢复全部内容,希望文章能够帮你解决java-如何使用RoboSpice从中断的未缓存请求中正确恢复所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)