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?
– viana
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.
– Henrique
Have you tried taking the
@Expose
and leave only the@SerializedName
?– viana
Yes, I tried it doesn’t change, I put it through internet searches. I didn’t use it
– Henrique
Bro, I think it’s because your json doesn’t have any "search"'... You’re using @Serializedname("search")...
– viana
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>
– viana
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.
– Henrique