I’m using retrofit API to get data, and for that, I’ve created the POJO classes as required.
Here are my POJO classes, 1. Report
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("data")
@Expose
private List<Datum> data = null;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
-
Datum
@SerializedName("_id") @Expose private String id; @SerializedName("seq") @Expose private Integer seq; @SerializedName("imei") @Expose private String imei; @SerializedName("lat") @Expose private String lat; @SerializedName("lon") @Expose private String lon; @SerializedName("etype") @Expose private String etype; @SerializedName("nos") @Expose private String nos; @SerializedName("rssi") @Expose private String rssi; @SerializedName("hac") @Expose private String hac; @SerializedName("spd") @Expose private String spd; @SerializedName("btv") @Expose private String btv; @SerializedName("address") @Expose private String address; @SerializedName("dt") @Expose private String dt; @SerializedName("erpm") @Expose private String erpm; @SerializedName("thpos") @Expose private String thpos; @SerializedName("erntm") @Expose private String erntm; @SerializedName("fgrl") @Expose private String fgrl; @SerializedName("dtml") @Expose private String dtml; @SerializedName("flvl") @Expose private String flvl; @SerializedName("ftype") @Expose private String ftype; @SerializedName("eot") @Expose private String eot; @SerializedName("ect") @Expose private String ect; @SerializedName("odm") @Expose private String odm; @SerializedName("date") @Expose private String date; @SerializedName("__v") @Expose private Integer v; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getSeq() { return seq; } public void setSeq(Integer seq) { this.seq = seq; } public String getImei() { return imei; } public void setImei(String imei) { this.imei = imei; } public String getLat() { return lat; } public void setLat(String lat) { this.lat = lat; } public String getLon() { return lon; } public void setLon(String lon) { this.lon = lon; } public String getEtype() { return etype; } public void setEtype(String etype) { this.etype = etype; } public String getNos() { return nos; } public void setNos(String nos) { this.nos = nos; } public String getRssi() { return rssi; } public void setRssi(String rssi) { this.rssi = rssi; } public String getHac() { return hac; } public void setHac(String hac) { this.hac = hac; } public String getSpd() { return spd; } public void setSpd(String spd) { this.spd = spd; } public String getBtv() { return btv; } public void setBtv(String btv) { this.btv = btv; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDt() { return dt; } public void setDt(String dt) { this.dt = dt; } public String getErpm() { return erpm; } public void setErpm(String erpm) { this.erpm = erpm; } public String getThpos() { return thpos; } public void setThpos(String thpos) { this.thpos = thpos; } public String getErntm() { return erntm; } public void setErntm(String erntm) { this.erntm = erntm; } public String getFgrl() { return fgrl; } public void setFgrl(String fgrl) { this.fgrl = fgrl; } public String getDtml() { return dtml; } public void setDtml(String dtml) { this.dtml = dtml; } public String getFlvl() { return flvl; } public void setFlvl(String flvl) { this.flvl = flvl; } public String getFtype() { return ftype; } public void setFtype(String ftype) { this.ftype = ftype; } public String getEot() { return eot; } public void setEot(String eot) { this.eot = eot; } public String getEct() { return ect; } public void setEct(String ect) { this.ect = ect; } public String getOdm() { return odm; } public void setOdm(String odm) { this.odm = odm; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public Integer getV() { return v; } public void setV(Integer v) { this.v = v; }
Here is how I tried to fetch the data in form of a Datum list using the Report class.
Here is the activity code…
Call<Report> call = RetrofitClient
.getInstance()
.getApi()
.getAlltheReports(reportModel);
call.enqueue(new Callback<Report>() {
@Override
public void onResponse(Call<Report> call, Response<Report> response) {
Log.d("responseBodyyyyyyyyy", String.valueOf(response.body()));
Report example = response.body();
if (example != null) {
Log.d("gettingNull", "DATA");
List<Datum> data = response.body().getData();
Log.d("dataaaaaaaaaaaaaaaa", String.valueOf(data));
} else {
Log.d("gettingNull", "Null");
}
}
@Override
public void onFailure(Call<Report> call, Throwable t) {
Log.d("error", String.valueOf(t));
}
});
In, the retrofit call reportModel is nothing but JSON data.
For reference,
ReportModel:
ReportModelJson reportModel = new ReportModelJson("353081090133664", "Asia/Kolkata", "02-06-2022", "02-10-2022");
And I’m posting the API call like this:-
@POST("getonlyselect")
Call<Report> getAlltheReports(
@Body ReportModelJson ReportModelJson
);
This is I tried to fetch the data, but instead of getting the proper response, I’m getting the packagename@xxxx.
For reference:
com.example.abcd.Screens.Models.Datum@42df5e8
The response that I’m getting is like this:-
{
"success": true,
"data": [
{
"_id": "61fed1d527824e063e318124",
"seq": 21,
"imei": "353081090133664",
"lat": "22.472927",
"lon": "70.080795",
"etype": "stop",
"nos": "5",
"rssi": "-51",
"hac": "1.5",
"spd": "2",
"btv": "12.34",
"address": "abcd",
"dt": "1644089809000",
"erpm": "N",
"thpos": "N",
"erntm": "N",
"fgrl": "N",
"dtml": "N",
"flvl": "N",
"ftype": "N",
"eot": "N",
"ect": "N",
"odm": "N",
"date": "2022-02-05T19:36:53.087Z",
"__v": 0
}
]
}
Please help me to find out the solution.
>Solution :
As I understand your code, there is not any error in the retrofit API call, There is an error in the printing value that you got from API.
As I can see in your code you have used the below code to print data that you got from API.
List<Datum> data = response.body().getData();
Log.d("dataaaaaaaaaaaaaaaa", String.valueOf(data));
Also, Data is an array that you are trying to print using Log.d. So if you want to print your data so you can set for loop to print every object data like below.
List<Datum> data = response.body().getData();
for(int l=0; l<data.size(); l++)
{
Log.d("dataaaaaaaaaaaaaaaa", data.get(l)._id())
}
I hope, this will help you to solve your issue.