Junior — Middle
Utilizou a biblioteca Retrofit para trabalhar com APIs nos seus projetos?
sobes.tech IA
Resposta da IA
Sim, utilizei a biblioteca Retrofit para interagir com a API REST em projetos Android. Retrofit simplifica a criação de pedidos HTTP, convertendo automaticamente as respostas JSON em objetos Java usando conversores (por exemplo, Gson).
Exemplo de uso do 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();
// processamento dos dados
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
// tratamento do erro
}
});
O Retrofit ajuda a escrever código limpo e de fácil manutenção para chamadas de rede.