0

i am new in Retrofit.. i am using retrofit 2, but when i my code step into call.enqueue the code just skip it doesn't into method onRespone or onFailure.

Here goes my code.

Model Class

public class MovieResult {

    @SerializedName("page")
    @Expose
    private Integer page;
    @SerializedName("results")
    @Expose
    private List<Movie> movieList = new ArrayList<Movie>();
    @SerializedName("total_results")
    @Expose
    private Integer totalResults;
    @SerializedName("total_pages")
    @Expose
    private Integer totalPages;

    Setter and Getter
}

Model - Movie

public class Movie {
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = new ArrayList<Integer>();
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("vote_count")
@Expose
private Integer voteCount;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;

Setter and Getter
}

Retrofit Client

public class MovieRetrofit {

    private static MovieService movieService;
    private final static String BASE_URL = "http://api.themoviedb.org/3/";

    public static MovieService getMovieService(){

        if(movieService == null){
            Retrofit service = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            movieService = service.create(MovieService.class);
        }

        return movieService;
    }

}

Retrofit Service

public interface MovieService {

    @GET("discover/movie?")
    Call<MovieResult> getMovies( @Query("api_key") String apiKey,
                                 @Query("sort_by") String sortBy );

}

Fragment Class

 public class MovieFragment extends Fragment {

        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;
        private MovieResult movieResult;
        private List<Movie> movieList;

        public MovieFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_popularmovie);
            mRecyclerView.setHasFixedSize(true);

            mLayoutManager = new GridLayoutManager(getActivity(), 2);
            mRecyclerView.setLayoutManager(mLayoutManager);

    //        Movie movie = new Movie();
    //        movie.setId(231);
    //        movie.setTitle("Captain America: Civil War");
    //        movie.setPosterPath("5N20rQURev5CNDcMjHVUZhpoCNC.png");
    //        movie.setBackdropPath("m5O3SZvQ6EgD5XXXLPIP1wLppeW.jpg");
    //
    //        List<Movie> listMovies = new ArrayList<Movie>();
    //        listMovies.add(movie);

            movieList = new ArrayList<Movie>();
            mAdapter = new MovieAdapter(getActivity(), movieList);
            mRecyclerView.setAdapter(mAdapter);

            return rootView;
        }

        @Override
        public void onStart() {
            super.onStart();
            updateMovies();
        }

        public void updateMovies(){
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String sortBy = pref.getString(getString(R.string.pref_sort_key), getString(R.string.pref_sort_popular));
            fetchMovies(sortBy);
        }

        public void fetchMovies(String sortBy){
            Call<MovieResult> call = MovieRetrofit.getMovieService().getMovies(BuildConfig.THE_MOVIE_DB_API_KEY, sortBy);
            call.enqueue(new Callback<MovieResult>() {
                @Override
                public void onResponse(Call<MovieResult> call, Response<MovieResult> response) {
                    movieResult = new MovieResult();
                    movieResult = response.body();

                    movieList.addAll(movieResult.getMovieList());
                }

                @Override
                public void onFailure(Call<MovieResult> call, Throwable t) {
                    Log.e("CALL FAILURE", t.getMessage());
                }
            });
        }
    }

JSON Respone

{  
   "page":1,
   "results":[  
      {  
         "poster_path":"\/5N20rQURev5CNDcMjHVUZhpoCNC.jpg",
         "adult":false,
         "overview":"Following the events of Age of Ultron, the collective governments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers, causing two factions to side with Iron Man or Captain America, which causes an epic battle between former allies.",
         "release_date":"2016-04-27",
         "genre_ids":[  
            28,
            878,
            53
         ],
         "id":271110,
         "original_title":"Captain America: Civil War",
         "original_language":"en",
         "title":"Captain America: Civil War",
         "backdrop_path":"\/m5O3SZvQ6EgD5XXXLPIP1wLppeW.jpg",
         "popularity":76.074932,
         "vote_count":323,
         "video":false,
         "vote_average":6.5
      }
   ],
   "total_results":19623,
   "total_pages":982
}

i've tried put call service in onCreateView but it failed. The problem is always same, when the code at call.enqueue it will not step into onRespone and onFailure. And i don't what the problem with it @@.

Code21K
  • 74
  • 2
  • In your Retrofit Builder, add this method setLogLevel(LogLevel.FULL) before build() and then try to figure out the reason by the help of logcat – Yasir Tahir May 01 '16 at 10:26
  • Sorry, you're using retrofit 2. For logging, please follow these steps: http://stackoverflow.com/questions/32514410/logging-with-retrofit-2 – Yasir Tahir May 01 '16 at 10:38
  • oke thanks @Yasir Tahir, ill try it.. btw i tried to run the app again and it is success... dunno whats the problem hooh... – Code21K May 01 '16 at 11:51

0 Answers0