DWA
DWA

Reputation: 530

Access to Return value in Response Body from Retrofit2 Call

Being new to Retrofit2 and having troubles in processing the Response Body from the Call - the needed Response is visible in the Android Studio Debugger, the data is there; however, due to lack of Java skills, it is unclear to me how to access the Data that I am receiving. It looks like type conversion is needed.

The data looks like this -

Log.d(TAG, "messageId" + addBookingMessageList.toString());

gives me

messageIdcom.example.xxx.AddBookingMessage@88fe1de

and

Log.d(TAG, "addBookingMessageList Class" + addBookingMessageList.getClass().getName());

returns

addBookingMessageList Classjava.lang.String

How do I parse this data correctly? I know it´s an Object, but it is unclear to me how to access the object properties.

Any hints or help would be very much appreciated.

Here´s my Retrofit Call:

  public static void putBookingOnPallet(String basic,
                                      AddBookingMessage message, Context mContext) {

    MessagesApi messageApi = retrofit.create(MessagesApi.class);
    //making the call objects
    Call<AddBookingMessage> call = messageApi.addBooking(basic, message);

    //call.execute();

    call.enqueue(new Callback<AddBookingMessage>() {
        @Override
        public void onResponse(@NonNull Call<AddBookingMessage> call, @NonNull
                Response<AddBookingMessage> response) {

            if (response.isSuccess()) {
                Object addBookingMessageList;
                addBookingMessageList = response.body().toString();
                Log.d(TAGGG, (String) addBookingMessageList.toString());
                Log.d(TAG, "messageId" + addBookingMessageList.toString());
                Log.d(TAG, "addBookingMessageList Class" + addBookingMessageList.getClass().getName());
                //Toast.makeText(context, statusCode, Toast.LENGTH_LONG).show();
            }
            else {
                int statusCode = response.code();
                Log.d(TAG, "statusCode" + statusCode);

            }
        }


        @Override
        public void onFailure(Call<AddBookingMessage> call, Throwable t) {
            Log.d(TAG, "t.getMessage" + t.getMessage());
            Toast.makeText(mContext, t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

Here´s my MessagesAPI:

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;


public interface MessagesApi {

    String BASE_URL = "xxx";

    @POST("message")
    @Headers({ "Content-Type: application/json;charset=UTF-8"})
    Call<AddBookingMessage> addBooking(
            @Header("Authorization")
                    String basic,
            @Body AddBookingMessage message
    );

    @POST("message")
    @Headers({ "Content-Type: application/json;charset=UTF-8"})
    Call<AddBookingMessage> removebooking(
            @Header("Authorization")
                    String basic,
            @Body AddBookingMessage message
    );
}

Here´s my Pojo:

package xxx;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AddBookingMessage {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("time")
    @Expose
    private Integer fromID;
    @SerializedName("fromID")
    @Expose
    private Integer toID;
    @SerializedName("toID")
    @Expose
    private Integer typeID;
    @SerializedName("typeID")
    @Expose
    private String title;
    @SerializedName("title")
    @Expose
    private String receiptNo;
    @SerializedName("receiptNo")
    @Expose
    private String note;
    @SerializedName("note")
    @Expose

/*    private String id;
    @SerializedName("id")
    @Expose*/

    private Long time;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void getTitle(String title) {
        this.title = title;
    }

    public Integer getFromId() {
        return fromID;
    }

    public void setFromId(Integer fromID) {
        this.fromID = fromID;
    }

    public Integer getToID() {
        return toID;
    }

    public void setToId(Integer toId) {
        this.toID = toId;
    }

    public Integer getTypeId() {
        return typeID;
    }

    public void setTypeId(Integer typeID) {
        this.typeID = typeID;
    }
    public String getReceiptNo() {
        return receiptNo;
    }

    public void setReceiptNo(String receiptNo) {
        this.receiptNo = receiptNo;
    }
   public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    /*public String getId() {
            return id;
    }

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

Again, any hints or help would be vey much appreciated as I am stuck here, Thanks in advance!

Upvotes: 0

Views: 1431

Answers (1)

Evelyn
Evelyn

Reputation: 2656

As @Chirag Rayani mentioned, you should have your pojo in the api definition and in the call. So you should have something like:

MessageApi: Change

Call<Object> addBooking(headers, etc...);

to

Call<AddBookingMessage> addBooking(headers, etc...);

And same for the call:

Call<AddBookingMessage> call = messageApi.addBooking(basic, message);

call.enqueue(new Callback<AddBookingMessage>() {
            @Override
            public void onResponse(@NonNull Call<AddBookingMessage> call, @NonNull
                    Response<AddBookingMessage> response) {

                if (response.isSuccess()) {
                    // use your object here
                    Log.v("response", "id " + response.body().getId());
                    ...
                }
            }

            @Override
            public void onFailure(Call<AddBookingMessage> call, Throwable t) {
                ...
            }
        });

Upvotes: 1

Related Questions