S
S
super_beginer2019-05-22 20:17:54
Android
super_beginer, 2019-05-22 20:17:54

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'

Here is the URL itself https://api.themoviedb.org/3/discover/movie?api_ke... In the interface, I requested it like this via get
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);
}

In MainActivity did this
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());

            }
        });

    }
}

I understand there is no synton in the interface, but that's not the point. I just need to check if the link works.
but in the log I get this 2019-05-22 22:55:43.287 17635-17635/com.example.testretrofit I/error: socket failed: EPERM (Operation not permitted)
Permissions are already enabled in the manifest.
<uses-permission android:name="android.permission.INTERNET"/>

Tell me what I'm doing wrong, the Internet has all the rules. test on the emulator.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question