简体   繁体   中英

Retrofit is returning an empty body

I'm trying to use retrofit & gson to parse some JSON. However, I am getting an empty response body. When I try to print information from the session object I get a NullPointerException.

I have made sure that the URL is correct and I'm sure my POJOs are correct as well. I am using jsonschema2pojo to help create the POJO classes.

Here is the JSON I'm trying to parse

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

This is my retrofit client / setup

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

Finally, this is the code in my 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);

            }
        }

and here is my 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;
}
}

but the messsage is always "" and error is always False and data has MiddlewareResponse but with null session and null form响应图像

I guess the teacher_id is not int inside Session , primitives cannot be null , there might be problem parsing the Json, try using it as String and same for Lat and Long inside Mosque , then convert String to int once you parse the response .

the problem was from me I forget to pull the back end code in the serve and the response I post in the question I discover it was from the local host not from the server it took from me three days to discover that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM