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 ?
Thanks in advance.
I am not sure to understand what an error is for you. It looks like you got some error handling in on success.
What kind of error do you expect to trigger the request failure of the listener ?
S.
--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "RoboSpice".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse robospice+...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.
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();
}
I think I get it.
The problem is that there is a confusion here about what you say an error is.
If you send a request, the server :
* returns a status code
* returns a json
Both in case of an error and a success...
So what you call an error at the application level is indeed a success at the network level.
To deal with that, some framework, like retrofit, expect a 200 return code and nothing else. The rest will be errors. In your case, you use Google httpclient. I have not used it for a while, but I think you should do the following :
In your spice request. After the execute request, get the status code and get the data. If it works (200& return data) then return the pojo directly from your request, your app doesn't want to care about an httpresponse what it wants is data. If the response code is not 200, then parse the json build an exception and throw it.
Rs is designed to let you play at a higher level. In your case you return a http response, but you will always get one, even with a 404 request !
S.
HttpResponse response;
try {
response = request.execute();
} catch (HttpResponseException hre) {
throw new SpiceException("Something went wrong ...");
}
if (response.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
throw new SpiceException("Something went wrong ...");
}
return response;
Google HTTP client itself throws an HTTPResponseException if the Response is not having the success status code. As above, I catch it and throw a SpiceException. Works well till here but then my onRequestFailure() is never hit after this point.
--
--
Vous recevez ce message, car vous êtes abonné à un sujet dans le groupe Google Groupes "RoboSpice".
Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/robospice/-u3QPRGqBBo/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse robospice+...@googlegroups.com.
public class FBUserSaveRequest extends GoogleHttpClientSpiceRequest<HttpResponse> {
private String fbLoginBeanJson;
private Context context;
public FBUserSaveRequest(String fbLoginBeanJson, Context context) {
super(HttpResponse.class);
this.fbLoginBeanJson = fbLoginBeanJson;
this.context = context;
}
@Override
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());