OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
public interface UserService {
@GET("/users")
Call<List<User>> getUsers(
@Query("per_page") int perPage,
@Query("page") int page);
@GET("/users/{username}")
Call<User> getUser(@Path("username") String username);
}
UserService service = retrofit.create(UserService.class);
Call<User> call = service.getUser("fonsi");
Call<User> callAsync = service.getUser("eugenp");
callAsync.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();
}
@Override
public void onFailure(Call<User> call, Throwable throwable) {
System.out.println(throwable);
}
});
Response<User> callSync = service.getUser("fonsi").execute();
if (response.isSuccessful()) {
// return response.body();
} else {
// handle API error
}