http status code even without error response retrofit - android

I'm working with Retrofit 1.9
I'm calling a webservice which never send response body, just a 200 when it's okey and a 404 when it's not.
To handle 202 success it's easy since the Retrofit callback already has a method "success" called when the code is between 200 and 300.
But the issue I have is with this part :
#Override
public void failure(RetrofitError error) {
error.getResponse().getStatus();
}
how should I get the http response code to check if it's a 404 or a 500(or whatever else) since the error.getResponse() is null ? I'm trying to do this to correctly notify the user.

Related

Android Retrofit + Rxjava: How to get response on non200 code?

This is how my request looks like:
ApiService apiService = retrofit.create(ApiService.class);
Observable<Response<UserUpdateResponse>> response = apiService.updateUser(Utils.getHeader(), object);
response.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onSuccessUpdate,
this::onErr,
this::hideDialogLoading);
It's supposed to return 'code':'205' 'msg':'successfully update'. But when server response any code 201,202 (anything not 200) it will go to error.
Here is the Error.
java.net.ProtocolException: HTTP 205 had non-zero Content-Length: 121
So how do I prevent it from error, or how do I get error body? Thank you!.
HTTP response codes have a predefined definition and some have requirements that they must fullfill to be considered a valid HTTP payload. You cannot redefine what these codes mean for your application and expect well-implemented clients to accept it.
Looking specifically at HTTP 205 - Reset Content, which has the following requirement:
Since the 205 status code implies that no additional content will be provided, a server MUST NOT generate a payload in a 205 response.
Generally applications will just return HTTP 200 for all requests and include application-specific error codes in the payload. What you're doing does not make much sense.
So technically, I can get response 2xx. The problem was that server response body in response code 205 that suppose to be null (https://www.rfc-editor.org/rfc/rfc7231#section-6.3.6). So after set body null on server, android side works fine.

How to send error message from node express to retrofit on android

I have a nodes server, and on my android device I'm using retrofit to talk to my API.
if I do a regular/succesfull response from server, res.json(User) or res.send("message"), I get the the response body on the android correctly.
public void onResponse(Call<User> call, Response<User> response) {
//response contains a body or message
}
HOWEVER, if I want to send an error, and use
res.status(500).json({"msg":"user not found");
or
res.status(500).send("user not found")
inside of my retrofit callback, I get a on success call back with the correct status code, in this case 500, but the error message I'm trying to send is not passed to me.
It seems that since the http response status was an error status, retrofit just ignores the response body/contents.
How can I send an error status 500, but also send data with it to retrofit?
I always use this approach:
res.send({status:200, "msg":"user not found"});

How do I get Robospice to treat anything other than a 200 response from Retrofit & OKHttp as an error

I am using Robospice on android with Retrofit and OKHttp. All works great with responses passed back to the activity using the Robospice RequestListener. The problem is that it only returns a failure if the connection times out or another network issue. If a 401 is returned then it is classed as a success but with a null response as it couldn't parse the JSON into our MobileAppSetup POJO.
At the moment I'm having to do null checking on the response but I have no idea what the reason was if it was a server error or a valid 401.
public final class HTTPRequestListener implements RequestListener<MobileAppSetup> {
#Override
public void onRequestFailure(SpiceException spiceException) {
Log.d("", "failure:"+ spiceException);
loginProgress.hide();
}
#Override
public void onRequestSuccess(final MobileAppSetup response) {
Log.d("","success. Data: "+response);
if(response==null)
showDialog("Error logging in", "Please check your username and password and try again.", "");
loginProgress.hide();
postLoginProcess(response);
}
}
I need to pass these errors to the onRequestFailure callback so I can properly handle it. Is there a way to specify error codes that Robospice should treat as an error. I think it involves adding some kind of custom error handler but really can't find a solution at the moment.
This due to the bug in OKHTTP client possible bug!
Problem is When the server answers with a 401 Unauthorized responses,
the Callback object calls its failure() method..
When the server returns with the 401 status, the RetrofitError
response object is null, and the networkError flag is set to true,
making it impossible for the app to check
error.getResponse().getStatus() I believe the error lies on the http
client. When using OkClient I get this error: java.io.IOException:
No authentication challenges found
I suggest you to download new okhttp jar file from square/okhttp run the project again! or try with any other client instead of okhttp.

Previous Android Response Being Added to Status Line

I'm using Volley to communicate with an API.
I am sending a request and on success of that request I immediately fire off another one, using the same message queue.
The issue I have is the second request is responding with the following error:
java.net.ProtocolException: Unexpected status line: {"id":47}HTTP/1.1 200 OK
The {"id":47} is the response body from the first request. I'm not even going near the status line in my code and the requests are fairly simple.
What on earth is happening?! Is it a bug within Volley?
I would suppose that the status line "HTTP/1.1 200 OK" is expected to be before the actual response content {"id":47} - according to the error message it seems to be exactly reversed.
In my case, the server return content when status code is 204,
api should not return data when 204 responses

How to stop retrying request on 401 using volley and okhttp

I'm using the new(ish) volley library for networking in my app. The server I'm communicating with returns 401s sometimes without proper challenge headers and this can't be changed.
I started using OkHttp as the transport layer for volley because I actually needed to read the response and there was an exception being thrown when I got a 401. Now though anytime I receive a 401 the request is automatically retried once before it gets to my error listeners. This is problematic for me as the response in the 401 changes on the second request and that's the one I get access to.
Is there any way to change this so it doesn't retry automatically when you receive a 401, or alternatively get access to the response just using volley?
Implement own RetryPolicy and override public void retry(VolleyError error) method like this:
#Override
public void retry(VolleyError error) throws VolleyError {
if (error.networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED)
{
throw new VolleyError("Client is not authorized, retry is pointless");
}
}

Categories

Resources