这是我的电影应用程序的“Client.java”代码,我已经传递了API的基本URL。
$1
下面是“Service.java”的代码,我正在使用@GET方法:
public static Retrofit getClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
以下是从服务器获取响应并加载JSON的代码。但我无法从服务器建立成功的响应:
@GET("movie/popular?api_key=")
Call<MoviesResponse> getPopularMovies(@Query("f48e0ffb5@@@@@@@@11f966fd71e8") String apikey) ;
@GET("movie/top_rated?api_key=")
Call<MoviesResponse> getTopRatedMovies(@Query("f48e0@@@@@@@@d1c11f966fd71e8") String apikey) ;
}
Gradle脚本:
private void loadJSON(){
Client Client = new Client();
Service service = Client.getClient().create(Service.class) ;
Call<MoviesResponse> call =service.getPopularMovies(API_KEY);
call.enqueue(new Callback<MoviesResponse>() {
@Override
public void onResponse(Call<MoviesResponse> call, Response<MoviesResponse> response) {
if(response.code()==200) {
List<Movie> movies = response.body().getResults();
Collections.sort(movies, Movie.BY_NAME_ALPHABETICAL);
recyclerView.setAdapter(new MoviesAdapter(getApplicationContext(), movies));
recyclerView.smoothScrollToPosition(0);
}else
{
Toast.makeText(MainActivity.this, "ERROR_FETCHING_DATA", Toast.LENGTH_SHORT).show();
}
}