Retrofit always returning null in Response

1

The answer comes with code 200, that is, correct, but the body is null.

Follow the model:

public class MovieModel {
    @SerializedName("imdbID")
    @Expose
    public String idMovie;

    @SerializedName("Title")
    @Expose
    public String titleMovie;

    @SerializedName("Year")
    @Expose
    public String yearMovie ;

    @SerializedName("Poster")
    @Expose
    public String imgMovie;

    public MovieModel() {
    }

    public MovieModel(String idMovie, String titleMovie, String yearMovie, String imgMovie) {
        this.idMovie = idMovie;
        this.titleMovie = titleMovie;
        this.yearMovie = yearMovie;
        this.idMovie = imgMovie;
    }

}

API:

public interface ApiRetrofitService {

    interface MoviesFutureCallback<T> {

        void onSuccess(T movies);
    }
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://www.omdbapi.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    @GET("?apiKey=ec6483bd")
    Call<MovieResults> getFilmesByName(@Query("t") String movieName);


}

Service:

ApiRetrofitService serviceapi = ApiRetrofitService.retrofit.create(ApiRetrofitService.class);

public void moviesSearchResult(final Context context, String nameMovie, final ApiRetrofitService.MoviesFutureCallback<MovieResults> callback){
    Call<MovieResults> call = serviceapi.getFilmesByName(nameMovie);
    call.enqueue(new Callback<MovieResults>() {
        @Override
        public void onResponse(Call<MovieResults> call, Response<MovieResults> response) {
            if (response.code() == 200){
                try{
                    MovieResults results = response.body();
                    callback.onSuccess(results);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<MovieResults> call, Throwable t) {
            Toast.makeText(context,"Failure",Toast.LENGTH_SHORT).show();
        }
    });
}

Results:

public class MovieResults {

    @SerializedName("search")
    public List<MovieModel> movies;

    public MovieResults(){}

    public MovieResults(List<MovieModel> movies) {
        this.movies = movies;
    }
}

PostMan Response:

{
    "Title": "Star",
    "Year": "2001",
    "Rated": "NOT RATED",
    "Released": "01 Jun 2001",
    "Runtime": "7 min",
    "Genre": "Action, Short, Comedy",
    "Director": "Guy Ritchie",
    "Writer": "Guy Ritchie, Joe Sweet",
    "Actors": "Clive Owen, Michael Beattie, Toru Tanaka Jr., DTeflon",
    "Plot": "The Driver now carries an arrogant rock star who is visiting a major city (not Pittsburgh as earlier believed). Played by Madonna, this title character wants to get away from her bodyguards...",
    "Language": "English",
    "Country": "USA",
    "Awards": "1 win.",
    "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTY0NTY2NTUwNV5BMl5BanBnXkFtZTYwNzQxMzg5._V1_SX300.jpg",
    "Ratings": [
        {
            "Source": "Internet Movie Database",
            "Value": "7.8/10"
        }
    ],
    "Metascore": "N/A",
    "imdbRating": "7.8",
    "imdbVotes": "6,459",
    "imdbID": "tt0286151",
    "Type": "movie",
    "DVD": "N/A",
    "BoxOffice": "N/A",
    "Production": "N/A",
    "Website": "N/A",
    "Response": "True"
}

Android debug response:

Response{protocol=http/1.1, code=200, message=OK, url=http://www.omdbapi.com/?apiKey=ec6483bd&t=Test}

Where is it wrong? Because it is always null.

    
asked by anonymous 25.07.2017 / 20:48

1 answer

1

It is returning null because the parse of the body in JSON is not happening for the Java object. Based on the method name Call<MovieResults> getFilmesByName(@Query("t") String movieName); the return must be a list of movies, so JSON should be in list format too, which does not match the returned in your test with Postman.

Your request to the API will always return a single result.

To return a list, you must change the t parameter to s , as documented in: link

And then, your JSON will look like this:

{
  "Search": [ 
    {
     "Title": "Star",
     "Year": "2001",
     "Rated": "NOT RATED",
      ...
    }, 
    {
      ...
    }
  ]
}

Ah, change @SerializedName("search") to @SerializedName("Search") .

    
25.07.2017 / 22:06