skip to Main Content

I am learnng how to use retrofit library,however I come across a little problem.

So I set everything and run my project but first I git this error:

Caused by: java.lang.IllegalArgumentException: baseUrl must end in /:

So I added the "/" but then I realized that its more than that, and I should leave just the baseUrl and add the api to the interface I created.

I tried to add the api in diffrent ways but I didn’t manage to do it.

Here are some codes:

Retrofit BaseUrl:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.nytimes.com/svc/movies/v2/critics/full-time.json?api-key=abcdefghijklmnop")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

At first I used it like above,but after some research I discovered that I have to leave the baseUrl and pass the other half of the address through the interface.

Interface –

public interface ConnectAPI {

    @GET("results")
    Call<List<Reviewers>> getReviewers();

}

I will be glad for some help,
Thanks !

3

Answers


  1. Base URL should be the root of all the queries you want to make on that interface. The rest should be part of the URL on the actual API interface. So for your example, base URL should probably be "https://api.nytimes.com/svc/movies/v2/&quot;. Although any subset of that, such as "https://api.nytimes.com/&quot; would also work as long as the interface has all the rest of the path. Basically when the actual HTTP request is made, the URL of the query is concatenated to the end of the base url.

    Login or Signup to reply.
  2. As Gabe Sechan said, when working with Retrofit you need to set a base url that will be the same for all api calls and then append the rest of the url on a per endpoint basis. If you change your code to the below you should be good

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.nytimes.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    

    and getReviewers() to

    public interface ConnectAPI {
    
        @GET("svc/movies/v2/critics/full-time.json?api-key=abcdefghijklmnop")
        Call<List<Reviewers>> getReviewers();
    
    }
    
    Login or Signup to reply.
  3. //ApiClient class for BaseUrl(Retrofit)
    public class ApiClient {
    
        private static String BASE_URL="";
    
        private static OkHttpClient getOkHttpClient(){
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(logging).addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();
                    HttpUrl originalHttpUrl = original.url();
                    HttpUrl url = originalHttpUrl.newBuilder().build();
                    Request.Builder requestBuilder = chain.request().newBuilder()
                            .addHeader("Content-Type","application/json")
                            .addHeader("timezone", TimeZone.getDefault().getID())
                            .url(url);
    
                    Request request = requestBuilder.build();
                    Response response = chain.proceed(request);
                    return response;
                }
            });
    
            return httpClient.connectTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .retryOnConnectionFailure(true)
                    .build();
        }
    
    
        private static Retrofit.Builder retrofitBuilder=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create());
    
    
        private static Retrofit retrofit=retrofitBuilder.build();
    
        private static ApiService apiService=retrofit.create(ApiService.class);
    
    
    
        public static ApiService getApiService(){
    
            return apiService;
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search