简体   繁体   English

从 Retrofit2 调用访问响应正文中的返回值

[英]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;作为 Retrofit2 的新手并且在处理来自调用的响应主体时遇到问题 - 所需的响应在 Android Studio 调试器中可见,数据就在那里; however, due to lack of Java skills, it is unclear to me how to access the Data that I am receiving.但是,由于缺乏 Java 技能,我不清楚如何访问我收到的数据。 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.我知道它是 Object,但我不清楚如何访问 object 属性。

Any hints or help would be very much appreciated.任何提示或帮助将不胜感激。

Here´s my Retrofit Call:这是我的 Retrofit 电话:

  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:这是我的 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:这是我的 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!再次感谢任何提示或帮助,因为我被困在这里,提前致谢!

As @Chirag Rayani mentioned, you should have your pojo in the api definition and in the call.正如@Chirag Rayani 提到的,您应该在 api 定义和通话中拥有您的 pojo。 So you should have something like:所以你应该有类似的东西:

MessageApi: Change MessageApi:更改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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