0

Robospice と Google HTTP クライアントを使用して RESTful リクエストをサーバーに送信する Android アプリがあります。結果が正常に返された場合はすべて正常に動作しますが、サービスから例外が返された場合、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();


    }

RESTful サービスの Robospice 実装で何か見逃していましたか?

4

1 に答える 1

1

フラグメントのライフサイクルをデバッグすることで、これを解決できました。

フラグメントのライフサイクルが変更されたため、spiceManagerがクリアされていることに気付きました。robospice の FAQ セクションを見て、とても役に立ちました。

フラグメントの onAttach メソッドと onDetach メソッドでそれぞれ spiceManager の開始と終了を行いました。

于 2015-10-26T06:17:44.453 に答える