A resposta vem com o código 200, ou seja, correta, mas o body fica nulo.
Segue a 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;
}
}
Resposta 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"
}
Resposta no debug Android:
Response{protocol=http/1.1, code=200, message=OK, url=http://www.omdbapi.com/?apiKey=ec6483bd&t=Test}
Onde está errado? Pois sempre fica nulo.
@Expose
e deixar somente o@SerializedName
?