response.body使用改进返回null:2.3.0

时间:2017-07-18 07:39:02

标签: retrofit2 android gson

我的json数据是这个

{
    "data":[
        {
            "id":"4",
            "totalOpns":"40000",
            "killedDrugPer":"320",
            "arrestedDrugPer":"17683",
            "houseVisited":"4000",
            "userSurrenderers":"45000",
            "pusherSurrenderers":"15000",
            "totalSurrenderers":"60000",
            "killedPNPPerOpns":"40",
            "woundedPNPPerOpns":"70",
            "killedAFPPerOpns":"100",
            "woundedAFPPerOpns":"10",
            "date":"2017-07-12 13:57:34.000"
        }
    ]
}

我的数据模型就是这个

package com.androidtutorialpoint.retrofitandroid;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;

public class Student implements Serializable {

    //Variables that are in our json

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("totalOpns")
    @Expose
    private String totalOpns;
    @SerializedName("killedDrugPer")
    @Expose
    private String killedDrugPer;
    @SerializedName("arrestedDrugPer")
    @Expose
    private String arrestedDrugPer;
    @SerializedName("houseVisited")
    @Expose
    private String houseVisited;
    @SerializedName("userSurrenderers")
    @Expose
    private String userSurrenderers;
    @SerializedName("pusherSurrenderers")
    @Expose
    private String pusherSurrenderers;
    @SerializedName("totalSurrenderers")
    @Expose
    private String totalSurrenderers;
    @SerializedName("killedPNPPerOpns")
    @Expose
    private String killedPNPPerOpns;
    @SerializedName("woundedPNPPerOpns")
    @Expose
    private String woundedPNPPerOpns;
    @SerializedName("killedAFPPerOpns")
    @Expose
    private String killedAFPPerOpns;
    @SerializedName("woundedAFPPerOpns")
    @Expose
    private String woundedAFPPerOpns;
    @SerializedName("date")
    @Expose
    private String date;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTotalOpns() {
        return totalOpns;
    }

    public void setTotalOpns(String totalOpns) {
        this.totalOpns = totalOpns;
    }

    public String getKilledDrugPer() {
        return killedDrugPer;
    }

    public void setKilledDrugPer(String killedDrugPer) {
        this.killedDrugPer = killedDrugPer;
    }

    public String getArrestedDrugPer() {
        return arrestedDrugPer;
    }

    public void setArrestedDrugPer(String arrestedDrugPer) {
        this.arrestedDrugPer = arrestedDrugPer;
    }

    public String getHouseVisited() {
        return houseVisited;
    }

    public void setHouseVisited(String houseVisited) {
        this.houseVisited = houseVisited;
    }

    public String getUserSurrenderers() {
        return userSurrenderers;
    }

    public void setUserSurrenderers(String userSurrenderers) {
        this.userSurrenderers = userSurrenderers;
    }

    public String getPusherSurrenderers() {
        return pusherSurrenderers;
    }

    public void setPusherSurrenderers(String pusherSurrenderers) {
        this.pusherSurrenderers = pusherSurrenderers;
    }

    public String getTotalSurrenderers() {
        return totalSurrenderers;
    }

    public void setTotalSurrenderers(String totalSurrenderers) {
        this.totalSurrenderers = totalSurrenderers;
    }

    public String getKilledPNPPerOpns() {
        return killedPNPPerOpns;
    }

    public void setKilledPNPPerOpns(String killedPNPPerOpns) {
        this.killedPNPPerOpns = killedPNPPerOpns;
    }

    public String getWoundedPNPPerOpns() {
        return woundedPNPPerOpns;
    }

    public void setWoundedPNPPerOpns(String woundedPNPPerOpns) {
        this.woundedPNPPerOpns = woundedPNPPerOpns;
    }

    public String getKilledAFPPerOpns() {
        return killedAFPPerOpns;
    }

    public void setKilledAFPPerOpns(String killedAFPPerOpns) {
        this.killedAFPPerOpns = killedAFPPerOpns;
    }

    public String getWoundedAFPPerOpns() {
        return woundedAFPPerOpns;
    }

    public void setWoundedAFPPerOpns(String woundedAFPPerOpns) {
        this.woundedAFPPerOpns = woundedAFPPerOpns;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

,界面就是这个

package com.androidtutorialpoint.retrofitandroid;

import retrofit2.Call;
import retrofit2.http.GET;

public interface RetrofitObjectAPI {

/*
 * Retrofit get annotation with our URL
 * And our method that will return us details of student.
*/
// @GET("JSONTESTING/index.php")
@GET("getJson")
Call<Student> getStudentDetails();
}

在我的主要是这个

private void getRetrofitObject() {
    HttpLoggingInterceptor.Level logLevel = HttpLoggingInterceptor.Level.BODY;

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(logLevel);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    // add your other interceptors …

    // add logging as last interceptor
    httpClient.addInterceptor(logging);  // <-- this is the important line!

    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(url)
        .addConverterFactory(GsonConverterFactory.create())
        .client(httpClient.build())
        .build();

    RetrofitObjectAPI service = retrofit.create(RetrofitObjectAPI.class);

    Call<Student> call = service.getStudentDetails();

    call.enqueue(new Callback<Student>() {
    @Override
        public void onResponse(Call<Student> call, Response<Student> response) {
            try {
                if (response.isSuccessful()) {
                    text_id_1.setText("StudentId  :  " + response.body().getId());
                    text_name_1.setText("StudentName  :  " + response.body().getTotalOpns());
                    text_marks_1.setText("StudentMarks  : " + response.body().getArrestedDrugPer());
                } else {
                    //unsuccessful response
                }
            } catch (Exception e) {
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<Student> call, Throwable t) {
            Log.d("onFailure", t.toString());
        }
    });
}

在我的logcat中,我可以看到json数据,但在我的response.body上返回null logcat

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您收到了回复,但反序列化存在问题。 原因是Call<Student>不是它将被序列化的类型,因为你得到了一个学生列表(JSONArray)。

添加此课程

<强> MyResponse

public class MyResponse {

@SerializedName("data")
@Expose
private List<Student> data = null;

public List<Student> getData() {
return data;
}

public void setData(List<Student> data) {
this.data = data;
}

}

并将您的电话改为:

@GET("getJson")
Call<MyResponse> getStudentDetails();

现在你将获得回复正文 以及如何回顾?

List<Student> students = response.body().getData();

这样做.. :))