In this blog, we will know how to convert JSON Array response data to a model through the Retrofit Callback?
As we know that 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.
The answer is that we just pass a List of model class and Retrofit convert JSON Array object to the corresponding Model class.
- Here is our JSON Array response get from the Server after calling a request API.
1234567891011121314151617181920[{"id": "1","title": "discount","type": "C","type_data": "167","content": "discount","status": "A","time": "1480417496"},{"id": "2","title": "Offer","type": "P","type_data": "12","content": "New Year Offer","status": "A","time": "1480417516"}]
- Here is our description function of API Interface.
123456@GET(MOBIKUL_NOTIFICATION_DATA)Call<List<NotificationResponse>> getNotificationData(@Header("Authorization") String authkey, @Query("lang_code") String langCode, @Query("currency_code") String currencyCode);
- Here is our Retrofit call function, that is called the API via Retrofit Client.
12345678public static void getNotificationData(Context mContext, Callback<List<NotificationResponse>> callback){Call<List<NotificationResponse>> call = RetrofitClient.getClient(mContext).create(ApiInterface.class).getNotificationData(Helper.getAuthToken(), AppSharedPref.getLanguageCode(mContext), AppSharedPref.getCurrencyCode(mContext));call.enqueue(callback);}