Issue
I'm developing an android app with android studio, and I have a rest api, which I developed with spring. And every time I use retrofit to retrieve something from the server I get null body. Even though my server sends the data.
I've tried ScalarsConverterFactory and GsonConverterFactory, I tried as well changing the type of my Call to String, to ResponseBody and to JsonObject
here I make the call:
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("my_url")
.build();
ServerService service = retrofit.create(ServerService.class);
Call<List<String>> call = service.getUserAlbums("addcookie");
Response<List<String>> response = call.execute();
ServerService.java
@GET("/cmu/getUserAlbums")
Call<List<String>> getUserAlbums(@Header("Cookie") String cookie);
Here is the code in the server
@RequestMapping(value = "/getUserAlbums", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.FOUND)
public List<String> getUserAlbums() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.getUserByUsername(auth.getName());
return userService.getUserByUsername(user.getUsername()).getAlbums();
}
I was expecting to get a list as a string, I get the HTTP code 302, and in my console I can see that the server returns the List, but I can't receive on android.
Solution
I fixed the issue, basically retrofit considers a response successful if the code is between 200 - 299, so as it was returning the 302, the response body I was looking for was in .errorBody().
Answered By - Mihail
Answer Checked By - Katrina (JavaFixing Volunteer)