Enviar y recibir datos en JSON con Retrofit

Uso de Retrofit para realizar peticiones GET, POST, PUT y DELETE

Cómo consumir una API y procesar la respuesta usando Retrofit

Define the endpoints

public interface RetrofitClient {
      //https://api.github.com/users/azemZejnil/repos is whole URL

     //"users/{user}/repos" is the part of URL will be added to base URL. 
    @GET("users/{user}/repos")
    //List<GithubRepo> is return type    @Path("user")String user is the parameter we will pass           
    Call<List<GithubRepo>> reposForUser(@Path("user")String user);
}

How to Send JSON Data in a POST Request in Android

public interface CommentsService {
    @POST("comments")
    Call<Comment> createComment(@Body Comment comment);

    @FormUrlEncoded
    @POST("comments")
    Call<Comment> createComment(@Field("title") String title, @Field("comment") String comment, @Field("author") String author);

    @FormUrlEncoded
    @POST("comments")
    Call<Comment> createComment(@FieldMap Map<String, String> fields);
}

Código en Github

Retrofit 2 CRUD Android Example

public interface UserService {

    @GET("user/")
    Call<List<User>> getUsers();

    @POST("add/")
    Call<User> addUser(@Body User user);

    @PUT("update/{id}")
    Call<User> updateUser(@Path("id") int id, @Body User user);

    @DELETE("delete/{id}")
    Call<User> deleteUser(@Path("id") int id);
}

Más información:

Android – JSON Parsing using Retrofit Library with Kotlin

Android – JSON Parsing Using Retrofit Library with Jetpack Compose

GitHub API Sample Android

This is a sample application built to demonstrate use of Clean Architecture tools.

Deja una respuesta