Varun Behl
Varun Behl

Reputation: 43

Unable to use retrofit android library getting error while compiling

I am using the below code to call the API. I have added custom key that wont work:

 String API_BASE_URL="http://api.themoviedb.org/3/movie/popular?  api_key=adfasdfasd/";

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    MoviesApi service = retrofit.create(MoviesApi.class);
    Call<List<Pictures>> Pics = service.listPictures();

I have added below dependencies for retrofit in gradle:

   compile 'com.squareup.retrofit2:retrofit:2.0.1'
   compile 'com.squareup.retrofit2:converter-gson:2.0.2'

This is my interface

   public interface MoviesApi {
     @GET("results")
     Call<List<Pictures>> listPictures();
   }

I'm getting:

Caused by: java.lang.IllegalArgumentException: baseUrl must end in /: http://api.themoviedb.org/3/movie/popular?api_key=adfasdfasd/ at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:496) at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:439) at com.example.varunbehl.mymovies.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5459)  at java.lang.reflect.Method.invoke(Native Method) 

I have added / at the end of BASE URL. Any solution?

Upvotes: 1

Views: 3952

Answers (5)

Serg Chernata
Serg Chernata

Reputation: 12400

In my case, the base URL literally had to be "http://api.themoviedb.org". It couldn't have any segments and parameters after the domain.

Then you can define endpoints:

public interface MoviesApi {
   @GET("/3/movie/popular?api_key=adfasdfasd/")
   Call<List<Pictures>> listPictures(@Query("api_key") String api_key);

Upvotes: 1

Linkang Ma
Linkang Ma

Reputation: 177

 String API_BASE_URL="http://api.themoviedb.org/";

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

public interface MoviesApi {
 @GET("/3/movie/popular")
 Call<List<Pictures>> listPictures(@Query("api_key")String api_key);
}

Upvotes: 0

KishuDroid
KishuDroid

Reputation: 5451

The problem is with the method of passing your URL:

You should pass the URL like below:

String API_BASE_URL="http://api.themoviedb.org/3/;

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

And your interface would be like this:

     public interface MoviesApi {
     @GET("/movie/popular?{api_key}/results")
     Response getMovieList(@Path("api_key") String api_key, Callback<Response> callback);

Hope it will help you.

Upvotes: 1

jdp037
jdp037

Reputation: 36

I believe the problem is your base URL having a query parameter in it. So when trying to use retrofit in the way you have it set up, the route you have defined would be like this

http://api.themoviedb.org/3/movie/popular?api_key=adfasdfasd/results

This is not a valid structure for your URLs, as query parameters must be the last part of it.

It is also good practice to use the highest-level URL without any actual routes as your BASE_URL, so in order to make this work, you might try something like this:

String API_BASE_URL="http://api.themoviedb.org/3/";
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
MoviesApi service = retrofit.create(MoviesApi.class);
Call<List<Pictures>> Pics = service.listPictures();

And then your interface would look like this:

public interface MoviesApi {
   @GET("movie/popular/results")
   Call<List<Pictures>> listPictures(@Query("api_key") String api_key);

}

You may also want to verify that your routes are in fact correct. You wont get a proper response from the API if you have the routes in your interface defined incorrectly.

Upvotes: 0

Jesse Cochran
Jesse Cochran

Reputation: 70

First I would double check the API documentation and be sure that you have the correct format for your query.

http://docs.themoviedb.apiary.io/#reference/discover/discovermovie/get

They have the popular movies as show below:

http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=YOUR API KEY GOES HERE

I would recommenced having the base url:

.baseurl(http://api.themoviedb.org/3/discover/movie)

Then appending your query parameters.

Upvotes: 0

Related Questions