Robospice异常从未在RequestListener中捕获

时间:2015-08-26 18:13:38

标签: java android robospice google-http-client

我有一个Android应用程序,它使用Robospice和Google HTTP客户端向服务器发送RESTful请求。 如果成功返回结果,则一切正常但如果从我的服务返回异常,则Robospice监听器不会捕获异常。

public final class FBUserSaveListener implements RequestListener<HttpResponse> {

        @Override
        public void onRequestFailure( SpiceException spiceException ) {

            if(progressDialog.isShowing())
            {
                progressDialog.dismiss();
            }

            Toast.makeText(getActivity(),
                    "Error: " + spiceException.getMessage(), Toast.LENGTH_SHORT)
                    .show();
            Intent i = new Intent(getActivity(), ErrorActivity.class);
            startActivity(i);
        }

        @Override
        public void onRequestSuccess(HttpResponse response) {

                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            if(response.getStatusCode() == AppConstants.HTTP_CODE_CREATED_201) {
                Intent intent = new Intent(getActivity(), PoolMainActivity.class);
                startActivity(intent);
            }
            else{
                //Request was sent successfully but the response was wrong
                // Redirect to error page
                Intent i = new Intent(getActivity(), ErrorActivity.class);
                startActivity(i);
            }
        }


    }

在上面的代码中,当外部服务返回异常时,onRequestFailure()根本没有被命中。

我的要求是:

public HttpResponse loadDataFromNetwork() throws Exception{

        String url = context.getString(R.string.BASE_SERVICE_URL) + context.getString(R.string.USER_FB_LOGIN_SAVE);

        HttpRequest request = getHttpRequestFactory()//
                .buildPostRequest(new GenericUrl(url), ByteArrayContent.fromString("application/json", fbLoginBeanJson));
        request.setParser(new com.google.api.client.json.jackson2.JacksonFactory().createJsonObjectParser());
         return request.execute();


    }

我是否遗漏了Robospice RESTful服务的实现?

1 个答案:

答案 0 :(得分:1)

我设法通过调试片段的生命周期来解决这个问题。

我意识到由于片段生命周期的变化, spiceManager 被清除了。在robospice看到了常见问题解答部分,这对我帮助很大。

分别在我片段的onAttach和onDetach方法中启动和结束spiceManager。