我为什么会收到此错误 - java.lang.IllegalStateException:期望一个字符串,但在第1行第2行路径$ BEGIN_OBJECT $

时间:2018-03-07 05:33:00

标签: android retrofit2 gson

这是我的回答 {"message":"Success","draft_id":"5a9f70021207b061128b4567"}

这是我的POJO课程:

public class SaveNoteModel {

    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("draft_id")
    @Expose
    private String draftId;

    public String getMessage() {
        return message;
    }

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

    public String getDraftId() {
        return draftId;
    }

    public void setDraftId(String draftId) {
        this.draftId = draftId;
    }
}

我使用了scalars retrofit2:converter-scalars:2.1.0,用于将所有响应转换为字符串。 这是改装电话

ApiInterface apiInterface = RetrofitClient.getStringClient(ApiConstants.SAVE).create(ApiInterface.class);
        Call<String> call = apiInterface.saveNoteCall(userid,pcontract,scontract,action_type,headline,description,mediaOption,media,msf_symbol,mediaUrl,fileName,mediaIcon);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d("Retrofit", String.format("Data is: %s", response.body()));

                String res  =  response.body().toString();
                //res=  "{"+ "object_: "+res+"}";
                SaveNoteModel menuModel = new Gson().fromJson(res, SaveNoteModel.class);
                if (menuModel != null) {
                    onResponseReceiveEvent.getSuccess(menuModel);
                } else {
                    onResponseReceiveEvent.getFailure();
                    Log.d("Retrofit", String.format("Error is: %s", response.errorBody()));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                call.cancel();
                onResponseReceiveEvent.getFailure();
                Log.d("Retrofit", String.format("Error is: %s", t));
            }

2 个答案:

答案 0 :(得分:0)

您正在获取对象而不是字符串。它预计在字符串中。您可以将返回类型更改为对象,也可以返回字符串。在JSON [...]表示数组中,{...}表示对象,[{...}]是包含一个对象的数组。尝试使用

Gson gson = new Gson();
Message[] message= gson.fromJson(json, Message[].class);
System.out.println(Arrays.toString(message));
System.out.println(message[0]);

答案 1 :(得分:0)

您应该尝试返回SaveNoteModel而不是字符串的对象更改您的代码,

ApiInterface apiInterface = RetrofitClient.getStringClient(ApiConstants.SAVE).create(ApiInterface.class);
    Call<SaveNoteModel> call = apiInterface.saveNoteCall(userid,pcontract,scontract,action_type,headline,description,mediaOption,media,msf_symbol,mediaUrl,fileName,mediaIcon);
    call.enqueue(new Callback<SaveNoteModel>() {
        @Override
        public void onResponse(Call<SaveNoteModel> call, Response<SaveNoteModel> response) {
            Log.d("Retrofit", String.format("Data is: %s", response.body()));

            String res  =  response.body().toString();
            //res=  "{"+ "object_: "+res+"}";
            SaveNoteModel menuModel = new Gson().fromJson(res, SaveNoteModel.class);
            if (menuModel != null) {
                onResponseReceiveEvent.getSuccess(menuModel);
            } else {
                onResponseReceiveEvent.getFailure();
                Log.d("Retrofit", String.format("Error is: %s", response.errorBody()));
            }
        }

        @Override
        public void onFailure(Call<SaveNoteModel> call, Throwable t) {
            call.cancel();
            onResponseReceiveEvent.getFailure();
            Log.d("Retrofit", String.format("Error is: %s", t));
        }

注意:还要将ApiInterface课程中的返回类型更改为SaveNoteModel