没有从改造 api 调用中得到正确的响应

Not getting the proper response from retrofit api call

我正在使用改造 API 来获取数据,为此,我根据需要创建了 POJO classes。

这是我的 POJO classes,1. 报告

@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;
}
  1. 基准

     @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;
     }
    

以下是我尝试使用 Report class.

Datum list 形式获取数据的方法

这是 activity 代码...

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));
                }
            });

其中,改装调用reportModel只不过是JSON数据。

供参考,

ReportModel:

ReportModelJson reportModel = new ReportModelJson("353081090133664", "Asia/Kolkata", "02-06-2022", "02-10-2022");

我正在这样发布 API 电话:-

@POST("getonlyselect")
Call<Report> getAlltheReports(
        @Body ReportModelJson ReportModelJson
);

这是我试图获取数据,但我没有得到正确的响应,而是得到 packagename@xxxx

供参考:

com.example.abcd.Screens.Models.Datum@42df5e8

我得到的回复是这样的:-

{
"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
    }
]

}

请帮我找出解决办法。

据我了解你的代码,改造 API 调用中没有任何错误,你从 API 获得的打印值有错误。

正如我在您的代码中看到的,您已使用以下代码打印从 API 获得的数据。

List<Datum> data = response.body().getData();
 Log.d("dataaaaaaaaaaaaaaaa", String.valueOf(data));

此外,Data 是您尝试使用 Log.d 打印的数组。所以如果你想打印你的数据,你可以设置 for 循环来打印每个对象数据,如下所示。

List<Datum> data = response.body().getData();
for(int l=0; l<data.size(); l++)
{
  Log.d("dataaaaaaaaaaaaaaaa", data.get(l)._id())
 }

希望这能帮助您解决问题。