java - 在房间数据库中插入用户时显示错误

标签 java android android-room

我正在制作一个应用程序,我使用房间作为数据库缓存,并使用retrofit2从服务器获取数据并将其保存在房间数据库中,但从服务器获取数据后无法插入房间数据库。

这里显示了一个异常: enter image description here

这是我的 pojo 类:

@Entity(tableName = "Users")
public class User {

@NonNull
@PrimaryKey
@SerializedName("_id")
private String _id;

@ColumnInfo(name = "name")
@SerializedName("name")
@Expose
private String name;

@ColumnInfo(name = "age")
@SerializedName("age")
@Expose
private String age;

public User(){}

public User(@NonNull String _id,String name, String age) {
    this._id = _id;
    this.name = name;
    this.age = age;
}

@NonNull
public String get_id() {
    return _id;
}

public void set_id(@NonNull String _id) {
    this._id = _id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}
}

JSON 响应

[
{
    "id": "5cf68fe7b439470017236249",
    "name": "Rhea",
    "age": "2"
},
{
    "id": "5d09006696a8470cbc7c34a2",
    "name": "Don",
    "age": "10"
},
{
    "id": "5d092d9858af5d22a80858bf",
    "name": "Roman",
    "age": "30"
},
{
    "id": "5d09e9976f3bad18b8fa54a0",
    "name": "Roman",
    "age": "30"
},
{
    "id": "5d09ea2ac127bd07b4b64f6f",
    "name": "Roman",
    "age": "30"
}
]

请有人告诉我发生了什么问题。任何帮助将不胜感激。

最佳答案

chnage 

@NonNull
@PrimaryKey
@SerializedName("_id") // key does not match with response key
private String _id;


to 


@NonNull
@PrimaryKey
@SerializedName("id")
private String _id;

关于java - 在房间数据库中插入用户时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57269018/

相关文章:

java - 源代码控制intellij代码风格设置

java - 将某物设置为 INSTANCE 而不是创建新对象是什么意思?

java - 将字符串传递给 onItemClick 函数

android - 调试数据库和 SQLite 文件不一致

java - 组织.hibernate.HibernateException : save is not valid without active transaction in my case

java - 获取同一 Java 应用程序的实例(如果该应用程序已在运行)

android - 如何使用uiautomation在android中的chrome浏览器中打开一个url

android - "Exception raised during rendering: column indices (start + span) mustn' t 超出了列数。 “

android-room - 字符从房间持久性android中的查询开始

rx-java2 - 如何在 android room 和 rxjava 2 中插入数据并获取 id 作为输出参数?