Search code examples
javaandroidrobospicegoogle-http-client

Robospice exception never caught in RequestListener


I have an android app which uses Robospice with Google HTTP client to send RESTful request to a server. All works fine if result is successfully returned but if an exception is returned from my service, the Robospice listener doesn't catch the exception.

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);
            }
        }


    }

In the above code, when the external service returns an exception, the onRequestFailure() is not hit at all.

My request is :

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();


    }

Did I miss something in the Robospice implementation of RESTful services ?


Solution

  • I managed to solve this by debugging my fragment's life cycle.

    I realized that the spiceManager was getting cleared due to the changes in my fragment's life cycle. Saw the FAQ section in robospice and it helped me a lot.

    Made the spiceManager start and end in the onAttach and onDetach methods of my fragment respectively.