Updated 25 August 2017
What is Retrofit Library and How to send PUT and POST method in the retrofit?
Retrofit Library is a REST Client for Android and Java by Square. It makes relatively easy to send and retrieve JSON (or other structured data) via rest Webservice. In Retrofit, we can configure which converter is used for the data serialization.
When we want to send JSON request data then we use @body annotation in our Request call and pass our request model with @body in the request, and set the content type application/json in the header.
We can also use @Field
Annotation, so, we must put @FormUrlEncoded
in our API call.
1 2 3 4 5 6 7 8 9 10 11 |
@POST(MOBIKUL_ADD_TO_CART_DATA) Call<AddToCartResponseModel> addtoCartPost( @Header("Authorization") String authkey , @Header("Content-Type") String contentType , @Path("customerId") String customerId , @Query("width") int width , @Query("lang_code") String langCode , @Query("currency_code") String currencyCode , @Body AddToCartRequest request ); |
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void addToCartPost(Context mContext, Callback<AddToCartResponseModel> callback, AddToCartRequest request){ Call<AddToCartResponseModel> call = RetrofitClient.getClient(mContext).create(ApiInterface.class).addtoCartPost( Helper.getAuthToken() ,"application/json" , AppSharedPref.getCustomerId(mContext) , Utils.getScreenWidth() , AppSharedPref.getLanguageCode(mContext) , AppSharedPref.getCurrencyCode(mContext) , request ); call.enqueue(callback); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class AddToCartRequest { @SerializedName("product_data") @Expose private ProductData productData; public ProductData getProductData() { return productData; } public void setProductData(ProductData productData) { this.productData = productData; } } |
1 |
{"product_data":{"amount":"1","extra":{},"product_id":"54","product_options":{}}} |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.