gson.fromJson返回null android retrofit和gson

时间:2017-01-30 11:30:27

标签: android retrofit gson

当我将json错误响应从改造转换为对象时,我得到null。

JSON字符串:

{"messageType":1,"hasErrors":true,"isSuccess":false,"message":"Invalid username or password"}

我创建的课程:

public class ValidationContainer {


@SerializedName("messageType")
@Expose
private int MessageType;

@SerializedName("hasErrors")
@Expose
private Boolean HasErrors;

@SerializedName("isSuccess")
@Expose
private Boolean IsSuccess;

@SerializedName("message")
@Expose
private String Message;


public ValidationContainer() {
}

public String getMessage() {
    return Message;
}

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

}

我的代码:

            public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {

                if (response.isSuccessful()) {
                    //TODO: Save response user properties to shared constants

                    Intent intent = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                } else if (response.code() == 400) {

                    Gson gson = new GsonBuilder().create();
                    ValidationContainer container = new ValidationContainer();

                    try {
                        Log.e(Tag, response.errorBody().string());
                        container = gson.fromJson(response.errorBody().string(), ValidationContainer.class);

                        Log.e(Tag, container.getMessage());

                    } catch (IOException e) {

                        Log.e(Tag, e.getMessage());

                    }
                }
            }

当我记录response.errorBody().string()时,我得到以下内容:

enter image description here

在build.gradle中

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.6.1'

我做错了什么?

3 个答案:

答案 0 :(得分:1)

根据以下链接找到解决方案:https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {

    if (response.isSuccessful()) {
        //TODO: Save response user properties to shared constants

        Intent intent = new Intent(getBaseContext(), MainActivity.class);
        startActivity(intent);
        finish();

    } else if (response.code() == 400) {

        Converter<ResponseBody, ValidationContainer> converter =
                retrofit.responseBodyConverter(ValidationContainer.class, new Annotation[0]);

        try {
            ValidationContainer error = converter.convert(response.errorBody());

            Toast.makeText(getBaseContext(), error.getMessage(), Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            Log.d(Tag, e.getMessage());
        }

    }
}

答案 1 :(得分:0)

请在日志中发布错误消息,我认为您必须实现Serializable接口

public class ValidationContainer implements Serializable {

}

答案 2 :(得分:-1)

请粘贴实际代码,因为我认为gson解析不需要任何IO异常进行解析,因为我可以看到你正在使用它。

在删除if (response.code() == 400)

后也会尝试