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

这是我的代码 .

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
}

我已尝试在onCreateView中设置调用服务,但失败了 . 问题总是一样的,当 call.enqueue 处的代码不会进入 onResponeonFailure 时 . 我不知道@@的问题 .