我有一个android应用程序,它使用Robospice与谷歌HTTP客户端发送RESTful请求到服务器。如果成功返回result,则一切正常,但如果从我的服务返回异常,则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服务实现中错过了什么吗?
我设法通过调试我的片段的生命周期来解决这个问题。
我意识到spiceManager由于我的片段生命周期的变化而被清除。看到了robospice的FAQ部分,它帮助了我很多。
使spiceManager分别在我的fragment的onAttach和onDetach方法中开始和结束