Junior — Middle
Have you used the Retrofit library for working with APIs in your projects?
sobes.tech AI
Answer from AI
Yes, I have used the Retrofit library for interacting with REST APIs in Android projects. Retrofit simplifies the creation of HTTP requests and automatically converts JSON responses into Java objects using converters (e.g., Gson).
Example of using Retrofit:
public interface ApiService {
@GET("users/{id}")
Call<User> getUser(@Path("id") int userId);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService api = retrofit.create(ApiService.class);
Call<User> call = api.getUser(1);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
User user = response.body();
// handle data
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
// handle error
}
});
Retrofit helps write clean and maintainable code for network calls.