Answer the question
In order to leave comments, you need to log in
What is the correct way to make a request for a complex URL in retrofit2?
Good day gentlemen. I recently started learning Retrofit2 in android (java).
Here is the version of the library
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
public interface IRetrofitInterface {
@GET("movie")
Call<Films> getAllFilms(
@Query("api_key") String api_key,
@Query("language") String language,
@Query("sort_by") String sort_By,
@Query("include_adult") boolean include_adult,
@Query("include_video") boolean include_video,
@Query("page") int page);
}
public class MainActivity extends AppCompatActivity {
public static final String BASE_URL = "https://api.themoviedb.org/3/discover/";
public static final String API_KEY = "45454655465465вашключ";
public static final String LANGUAGE = "ru-RU";
public static final String SORT_BY = "popularity.desc";
public static final boolean INCLUDE_ADULT=false;
public static final boolean INCLUDE_VIDEO=false;
public static final int PAGE = 1;
private List<Result> movies = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parseJson();
}
private void parseJson() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
IRetrofitInterface retrofitInterface = retrofit.create(IRetrofitInterface.class);
Call<Films> call;
call = retrofitInterface.getAllFilms(API_KEY,LANGUAGE,SORT_BY,INCLUDE_ADULT,INCLUDE_VIDEO,PAGE);
call.enqueue(new Callback<Films>() {
@Override
public void onResponse(Call<Films> call, Response<Films> response) {
if(response.isSuccessful()&& response.body().getResults() !=null){
if(!movies.isEmpty()){
movies.clear();
}
movies = response.body().getResults();
for (Result movie :movies){
String title = movie.getTitle();
Log.i("title",title);
}
}
}
@Override
public void onFailure(Call<Films> call, Throwable t) {
Log.i("error",t.getMessage());
}
});
}
}
<uses-permission android:name="android.permission.INTERNET"/>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question