改造服务不会调用onRespone和onFailure

时间:2016-05-01 10:16:50

标签: android retrofit2

我是Retrofit的新手.. 我正在使用改造2,但当我的代码步入call.enqueue时,代码只是跳过它而不是方法onResponeonFailure

这是我的代码。

模型类

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
}

模特 - 电影

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
}

改造客户

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

}

改造服务

public interface MovieService {

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

}

片段类

 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
}

我已尝试在onCreateView中设置调用服务,但失败了。问题始终相同,当call.enqueue处的代码不会进入onResponeonFailure时。我不知道它有什么问题@@。

0 个答案:

没有答案