I'm using Volley to call api but it always returns "Failed" String in the onResponse method.
I checked the request object and response object and they are the same in postman, post man returns result but volley return "Failed".
Here's the call in fragment
And the call using volley
Related
I am making a POST request using Retrofit, call executes successfully on rest client. Also onResponse of call executes but in response body i am getting error
'unicode' object has no attribute 'items'.
What is the reason of this error?
I have an API observable returning a CUSTOM_MODEL. I would like to get the response headers from this call too.
Change your retrofit definition to return Observable<Response<CUSTOM_MODEL>>. The headers can be accessed through the Response object and CUSTOM_MODEL will be available through Response.body().
I am using Retrofit 2.1.0 library, and use call.enqueue method to do async API calls.
The CallBack class returns onResponse(Call call, Response response) and onFailure (Call call, Throwable t).
My question is that, is it safe to assume that the Response ( not response.body) object in the onResponse method will always be NonNull?
If it can be null, what can be the possible scenarios?
I looked online and on Retrofit docs, but no clear answers.
It shouldn't be null.
You can see in Retrofit source code that you only get a Response.success or a Reponse.error returned.
You can check if the HTTP request was a success using Response.isSuccessful().
If the Response was possible to be null, then that method would throw a NullPointerException.
If there was a scenario for a null Response, or some exception thrown when parsing the response, then onFailure would be entered.
I'm using Retrofit to get some data from a server. The problem is the response body is null (although I get a response code : 200 ) and if you check this github issue like I did you will see that they suggest you to use Call<Void> for empty response's body.
I did change it to Call<Void> and now when I'm making a request with Retrofit it enters onResponse and response is successful, but then I can't deserialize it with Gson.
Using :
DataServiceResponse serviceResponse = gson.fromJson(response.body(), DataServiceResponse.class);
is not possible since response.body() is void.
In my API server returns HTTP 400 response code if request does not pass validation, and provides detailed message, that should be parsed as the response.
For example:
public class RegistrationResponse {
private String emailError; // Detailed message. Null if no error occured
}
But Robospice (Retrofit + OkHttp) fires onRequestFailure() with message "retrofit.RetrofitError: 400 BAD REQUEST" in this case and, of course, does not parse anything.
How should I make it parse the response in case if response code is not 2XX?
You should declare Retrofit methods that return HTTP Response objects and check the raw object in your loadDataFromNetwork() for the status you need. This way, however, you will skip the out-of-the-box functionality of parsing responses and will have to do that manually.
Therefore, you should also find a way to reuse the Converter passed to your RestAdapter in the RetrofitSpiceService. Overriding the RetrofitSpiceService#createConverter() method is probably the simplest way to achieve this.