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
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/". Although any subset of that, such as "https://api.nytimes.com/" 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.
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
and
getReviewers()
to