Retrofit always returning null in Response

Asked

Viewed 349 times

1

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

Follows 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;
    }
}

Reply Postman:

{
    "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"
}

Reply in Android debug:

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

Where is it wrong? For it is always null.

  • It’s falling into the onFailure?

  • No. It normally falls on onSuccess, but returns null. I will edit the answer I get when using Postman, but in the code it is null.

  • Have you tried taking the @Expose and leave only the @SerializedName ?

  • Yes, I tried it doesn’t change, I put it through internet searches. I didn’t use it

  • Bro, I think it’s because your json doesn’t have any "search"'... You’re using @Serializedname("search")...

  • Because it should actually be an array, and you’ve only published one object. The array should have the name search, otherwise I imagine it won’t work at all. If it really is an object, you cannot put Call<Movieresults> and yes Call<Moviemodel>

  • You don’t have that little hand on Whatsapp’s face. Looks like you’re right, you don’t return me anyway, so you brought the card to the screen.

Show 2 more comments

1 answer

1


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

Your API request will always return a single result.

To return a list, you must change the parameter t for s, as the documentation in : http://www.omdbapi.com/

And then your JSON will look like this:

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

Ah, alter @SerializedName("search") for @SerializedName("Search").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.