java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 25 path $.posts application i get this error how to clear this error my json url https://kalviaruvi.com/calender/Tamilcalender/api/api.php?get_Today&Date_Number=2023-01-01&api_key=cda11Uib7PLEA8pjKehSVfY0vdHsXI269J3MlqcGatWZBmxOgR this
This api interface
@Headers({CACHE, AGENT})
@GET("api.php?get_Today")
Call<CallbackRecipeDetaildaily> getToday(
@Query("Date_Number") String Date_Number,
@Query("api_key") String api_key
);
This requestPostData
private void requestPostData() {
ApiInterface apiInterface = RestAdapter.createAPI(sharedPref.getApiUrl());
callbackCall = apiInterface.getToday(formattedDate,AppConfig.REST_API_KEY);
this.callbackCall.enqueue(new Callback<CallbackRecipeDetaildaily>() {
public void onResponse(Call<CallbackRecipeDetaildaily> call, Response<CallbackRecipeDetaildaily> response) {
CallbackRecipeDetaildaily responseHome = response.body();
if (responseHome == null || !responseHome.status.equals("ok")) {
onFailRequest();
return;
}
displayAllData(responseHome);
swipeProgress(false);
lyt_main_content.setVisibility(View.VISIBLE);
}
public void onFailure(Call<CallbackRecipeDetaildaily> call, Throwable th) {
Log.e("onFailure", th.getMessage());
if (!call.isCanceled()) {
onFailRequest();
}
}
});
} this displayalldata
public void displayData(final Recipe posts) {
txt_Date_Text.setText(posts.Date_Text);
txt_Date_Number.setText(posts.Date_Number);
txt_Date_Tamil.setText(posts.Date_Tamil);
txt_tv_viratham_time_range.setText(posts.tv_viratham_time_range);
txt_Datenaaltags.setText(posts.Datenaaltags);
txt_nnmorning.setText(posts.nnmorning);
txt_nnevening.setText(posts.nnevening);
txt_ekeiragu.setText(posts.ekeiragu);
txt_ekakuligai.setText(posts.ekakuligai);
}
this current pass api value1 pass
private String formattedDate;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = df.format(c.getTime());
this callback public class CallbackRecipeDetaildaily {
public String status = "";
public int count = -1;
public Recipe post = null;
}
This model recipe
public class Recipe implements Serializable {
public int id;
public String cat_id;
public String category_name;
public String last_update;
public String recipe_id;
public String Date_Text;
public String Date_Number;
public String recipe_image;
public String category_newsimage;
public String Date_Tamil;
public String tv_viratham_time_range;
}
CodePudding user response:
I assume CallbackRecipeDetaildaily is your model class that you are receiving. And in the response, you are getting list of posts which you are consuming it here in Recipe model class. However, you are getting exception because in response it is a list but here you are just added a single object of Recipe. Change it to List and the error will not come.
Also, rename the variable post to posts as you are receiving it in the response
CallbackRecipeDetaildaily {
public String status = "";
public int count = -1;
public List<Recipe> posts = null;
}
Make sure to modify the logic setting of data to use list instead of single object i.e. either call displayData inside for loop that iterates over the list or pass the list in the method and use for loop inside the method to set the data.
CodePudding user response:
Your API response is having Json Array of Recipe model so you need to add List<Recipe>
.