Retrofit take List data as POST request body in android rest api call

Retrofit take List data as POST request body in android rest api call

Step 1 : 

create a request model that will take request body data

package com.adn.api.order;

import com.adn.Model.DBModel.AddToCartProductModel;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;

public class Order {

@SerializedName("customer_id")
String customer_id;
@SerializedName("firstname")
String firstname;
@SerializedName("lastname")
String lastname;
@SerializedName("email")
String email;
@SerializedName("products")
ArrayList<AddToCartProductModel> products;
@SerializedName("telephone")
String telephone;
@SerializedName("postcode")
String postcode;
@SerializedName("payment_method")
String payment_method;
@SerializedName("total_price")
String total_price;
@SerializedName("address")
String address;
@SerializedName("city")
String city;
@SerializedName("order_status")
String order_status;

//Don't forget to create/generate the getter and setter]

public Order(String customer_id, String firstname, String lastname, String email, ArrayList<AddToCartProductModel> products, String telephone, String postcode, String payment_method, String total_price, String address, String city, String order_status) {
this.customer_id = customer_id;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.products = products;
this.telephone = telephone;
this.postcode = postcode;
this.payment_method = payment_method;
this.total_price = total_price;
this.address = address;
this.city = city;
this.order_status = order_status;
}

public String getCustomer_id() {
return customer_id;
}

public void setCustomer_id(String customer_id) {
this.customer_id = customer_id;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public ArrayList<AddToCartProductModel> getProducts() {
return products;
}

public void setProducts(ArrayList<AddToCartProductModel> products) {
this.products = products;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getPostcode() {
return postcode;
}

public void setPostcode(String postcode) {
this.postcode = postcode;
}

public String getPayment_method() {
return payment_method;
}

public void setPayment_method(String payment_method) {
this.payment_method = payment_method;
}

public String getTotal_price() {
return total_price;
}

public void setTotal_price(String total_price) {
this.total_price = total_price;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getOrder_status() {
return order_status;
}

public void setOrder_status(String order_status) {
this.order_status = order_status;
}

@Override
public String toString() {
return "Order{" +
"customer_id='" + customer_id + '\'' +
", firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", email='" + email + '\'' +
", products=" + products +
", telephone='" + telephone + '\'' +
", postcode='" + postcode + '\'' +
", payment_method='" + payment_method + '\'' +
", total_price='" + total_price + '\'' +
", address='" + address + '\'' +
", city='" + city + '\'' +
", order_status='" + order_status + '\'' +
'}';
}
}

Step 2 :

Add GSON on retrofit client call


package com.adn.api;

import com.adn.interfaces.URLs;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient implements URLs {
private static Retrofit getRetrofitInstance() {

Gson gson = new GsonBuilder()
.setLenient()
.create();

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.build();

return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}

/**
* Get API Service
*
* @return API Service
*/
public static AllApiCall getApiService() {
return getRetrofitInstance().create(AllApiCall.class);
}
}

Step 3 :

Call api like this in interface class


@POST("placeOrder")
Call<OrderContent> placeOrder(@Body Order order);

Step 4 :
Now Callback the api like this

Order order = new Order(userInfo.getId().toString(),userInfo.getFirstname().toString(),userInfo.getLastname(),userInfo.getEmail(),productModels,userInfo.getPhoneNumber(),postcode.getText().toString(),"cash in","45",address.getText().toString(),city.getText().toString(),"1");

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://mobileteam.adnarchive.com/opencartAPI/public/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();

AllApiCall service = retrofit.create(AllApiCall.class);

Call<OrderContent> orderContentCall = service.placeOrder(order);

orderContentCall.enqueue(new Callback<OrderContent>() {
@Override
public void onResponse(Call<OrderContent> call, Response<OrderContent> response) {
Toast.makeText(context, response.body().toString(), Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(Call<OrderContent> call, Throwable t) {

Toast.makeText(context, t.toString(), Toast.LENGTH_SHORT).show();

}
});

.

Done 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *