对于嵌套字段,Retrofit + Robospice JSON返回带有Null的POJO

时间:2016-01-21 09:46:43

标签: java gson retrofit pojo robospice

在我的项目中,我使用Robospice + Retrofit与remoute服务器交换数据,一切都很完美但有一个小问题, - 其中一个POJO对象每次都有嵌套字段为空。

POJO班

public class Snapshot {

@SerializedName("records_count")
private Integer recordsCount;

@SerializedName("created")
private String created;

@SerializedName("modified")
private String modified;

@SerializedName("records")
private Records records;

@SerializedName("database_id")
private String databaseId;

@SerializedName("revision")
private Integer revision;

@SerializedName("size")
private Integer size;

public Integer getRecordsCount() {
    return recordsCount;
}

public void setRecordsCount(Integer recordsCount) {
    this.recordsCount = recordsCount;
}

public String getCreated() {
    return created;
}

public void setCreated(String created) {
    this.created = created;
}

public String getModified() {
    return modified;
}

public void setModified(String modified) {
    this.modified = modified;
}

public Records getRecords() {
    return records;
}

public void setRecords(Records records) {
    this.records = records;
}

public String getDatabaseId() {
    return databaseId;
}

public void setDatabaseId(String databaseId) {
    this.databaseId = databaseId;
}

public Integer getRevision() {
    return revision;
}

public void setRevision(Integer revision) {
    this.revision = revision;
}

public Integer getSize() {
    return size;
}

public void setSize(Integer size) {
    this.size = size;
}

}

改造课程

public class RetrofitSpiceService extends RetrofitGsonSpiceService {

private static final String TAG = "RetrofitSpiceService";

private static final String AUTHORIZATION_HEADER = "Authorization";
private static final String USER_AGENT_HEADER = "User-Agent";
private static final String USER_AGENT = "Cloud API Android Client Example/1.0";

@Override
protected String getServerUrl() {
    return "https://my.cloud-api.net";
}

@Override
public void onCreate() {
    super.onCreate();
    addRetrofitInterface(DataSyncAPI.class);
}

@Override
protected RestAdapter.Builder createRestAdapterBuilder() {

    RestAdapter.Builder builder = new RestAdapter.Builder();

    builder.setRequestInterceptor(new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {

            request.addHeader(USER_AGENT_HEADER, USER_AGENT);
            request.addHeader(AUTHORIZATION_HEADER, SharedPreferencesManager.getRegistrationToken(getApplicationContext()));

        }

    });

    builder.setEndpoint(getServerUrl());
    builder.setConverter(getConverter());

    return builder;
}

}

那么,Snapshot类中的字段记录等于 null ,为什么?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用

添加GSON转换器?
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://localhost/api")
.setConverter(new GsonConverter(gson)).build();