簡體   English   中英

Retrofit 正在返回一個空的身體

[英]Retrofit is returning an empty body

我正在嘗試使用 retrofit 和 gson 來解析一些 JSON。 但是,我得到一個空的響應體。 當我嘗試從 session object 打印信息時,我得到了 NullPointerException。

我確保 URL 是正確的,並且我確信我的 POJO 也是正確的。 我正在使用 jsonschema2pojo 來幫助創建 POJO 類。

這是我要解析的 JSON

{
"error": false,
"message": "",
"data": {
    "form": null,
    "session": {
        "id": 8,
        "name_en": "جبر الصمادي",
        "name_ar": "عبداللطيف الجوالدة",
        "mosque_id": 2,
        "teacher_id": null,
        "session_type_id": 2,
        "start_date": "2021-02-13",
        "end_date": "2021-04-14",
        "register_available": 1,
        "brief_en": "Sequi sunt id voluptate eius veniam consectetur temporibus. Officia dolorem repudiandae optio autem iure. Voluptatem impedit eius alias voluptatem a et.",
        "brief_ar": "Error voluptatum est labore ipsam. Quasi beatae quo tenetur quia. Aut rerum hic rerum et quia error reiciendis doloribus. Aut voluptatibus explicabo autem et.",
        "image": "https://via.placeholder.com/640x480.png/00ddcc?text=voluptatem",
        "reason_registry_suspension": "Et dolorum sed voluptas recusandae cum. Odio ut et est vel sunt. Quo molestiae vel et cum odit.",
        "created_at": "2021-01-24T09:05:59.000000Z",
        "updated_at": "2021-01-24T09:05:59.000000Z",
        "mosque": {
            "id": 2,
            "image": "https://via.placeholder.com/640x480.png/00ee22?text=occaecati",
            "name": "عيدالله المومنى",
            "brief_location_description": "Facere natus.",
            "full_location_description": "Provident qui incidunt nobis ut possimus. Qui atque quod dolor iure enim nesciunt. Voluptate quia autem nesciunt.",
            "lat": null,
            "long": null,
            "created_at": "2021-01-24T09:05:58.000000Z",
            "updated_at": "2021-01-24T09:05:58.000000Z"
        }
    }
}
}

這是我的 retrofit 客戶端/設置

 public static Retrofit getClient(){
    Retrofit retrofit = new Retrofit.Builder()
            //.baseUrl("http://api.learn2crack.com/")
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    return retrofit;
}

最后,這是我的 onCreateView 中的代碼

  loginCall.enqueue(new Callback<SessionByIdResponse >() {

        @Override
        public void onResponse(Call<SessionByIdResponse > call, Response<SessionByIdResponse > response) {
            loading.dismiss();
            if (response.isSuccess()) {
                if (response.body().getError()) {
                    Utility.showAlertDialog(context.getString(R.string.error), response.body().getMessage(), context);

                } else {
                    Log.i(TAG, "token: " +  response.body().toString());
                    if (response.body().getData().getForm() !=null){

                    }else {
                        SessionDetails sessionDetails = new 
SessionDetails(response.body().getData().getSession());
                        ((MainActivity)context). 
getSupportFragmentManager().beginTransaction().replace(R.id.frame, sessionDetails).commit();

                    }
                }
            } else {
                Log.i(TAG, response.errorBody().toString());
                Utility.showAlertDialog(context.getString(R.string.error), 
context.getString(R.string.servererror), context);

            }
        }

這是我的 model

public class SessionByIdResponse {

@SerializedName("error")
private boolean error;

@SerializedName("message")
private String message;

@SerializedName("data")
private MiddlewareResponse data;

public boolean isError() {
    return this.error;
}

public void setError(boolean error) {
    this.error = error;
}

public String getMessage() {
    return this.message;
}

public void setMessage(String message) {
    this.message = message;
}

 public MiddlewareResponse getData() {
    return data;
}

public void setData(MiddlewareResponse data) {
    this.data = data;
}

public  class MiddlewareResponse {


@SerializedName("form")
private Form form;

@SerializedName("session")
private Session session;

public Form getForm() {
    return form;
}

public void setForm(Form form) {
    this.form = form;
}

public Session getSession() {
    return session;
}

public void setSession(Session session) {
    this.session = session;
}
}

public class Session {


@SerializedName("id")
private int id;
@SerializedName("name_en")
private String name_en;
@SerializedName("name_ar")
private String name_ar;
@SerializedName("mosque_id")
private int mosque_id;
@SerializedName("teacher_id")
private int teacher_id;
@SerializedName("session_type_id")
private int session_type_id;
@SerializedName("start_date")
private String start_date;
@SerializedName("end_date")
private String end_date;
@SerializedName("register_available")
private int register_available;
@SerializedName("brief_en")
private String brief_en;
@SerializedName("brief_ar")
private String brief_ar;
@SerializedName("image")
private String image;
@SerializedName("reason_registry_suspension")
private String reason_registry_suspension;
@SerializedName("created_at")
private String created_at;
@SerializedName("updated_at")
private String updated_at;
@SerializedName("mosque")
// private List<Mosques> mosques;
private  Mosque mosque;

public int getId() {
    return id;
}

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

public String getName_en() {
    return name_en;
}

public void setName_en(String name_en) {
    this.name_en = name_en;
}

public String getName_ar() {
    return name_ar;
}

public void setName_ar(String name_ar) {
    this.name_ar = name_ar;
}

public String getStart_date() {
    return start_date;
}

public void setStart_date(String start_date) {
    this.start_date = start_date;
}

public String getEnd_date() {
    return end_date;
}

public void setEnd_date(String end_date) {
    this.end_date = end_date;
}

public String getUpdated_at() {
    return updated_at;
}

public void setUpdated_at(String updated_at) {
    this.updated_at = updated_at;
}

public String getCreated_at() {
    return created_at;
}

public void setCreated_at(String created_at) {
    this.created_at = created_at;
}

public String getReason_registry_suspension() {
    return reason_registry_suspension;
}

public void setReason_registry_suspension(String reason_registry_suspension) 
{
    this.reason_registry_suspension = reason_registry_suspension;
}

public int getMosque_id() {
    return mosque_id;
}

public void setMosque_id(int mosque_id) {
    this.mosque_id = mosque_id;
}

public int getSession_type_id() {
    return session_type_id;
}

public void setSession_type_id(int session_type_id) {
    this.session_type_id = session_type_id;
}

public int getRegister_available() {
    return register_available;
}

public void setRegister_available(int register_available) {
    this.register_available = register_available;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public int getTeacher_id() {
    return teacher_id;
}

public void setTeacher_id(int teacher_id) {
    this.teacher_id = teacher_id;
}

public String getBrief_en() {
    return brief_en;
}

public void setBrief_en(String brief_en) {
    this.brief_en = brief_en;
}

public String getBrief_ar() {
    return brief_ar;
}

public void setBrief_ar(String brief_ar) {
    this.brief_ar = brief_ar;
}

public Mosque getMosque() {
    return mosque;
}

public void setMosque(Mosque mosque) {
    this.mosque = mosque;
}
}

但消息始終為“”且錯誤始終為 False 且數據具有 MiddlewareResponse 但具有 null session 和 null 形式響應圖像

我猜Session teacher_id int詞不能為null ,可能會有response的范圍和范圍Long Lat int String Mosque String綜合征。

問題出在我身上,我忘記在服務中提取后端代碼,並且我在問題中發布的響應我發現它來自本地主機而不是來自服務器,我花了三天時間才發現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM