在 Room 中插入 Retrofit 响应主体
Insert Retrofit response body in Room
我试图在 Room 中插入 response.body()
,这是一个 User
,但我得到
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 23714 (AsyncTask #2)
它显示在 RecyclerView
中,但是当我尝试缓存它时我的应用程序崩溃了。
这是代码
private void refreshUser() {
executor.execute(new Runnable() {
@Override
public void run() {
try {
retrofit2.Response<User> response=webservice.getUser("1").execute();
User fromNetwork=response.body();
mUserDao.insertUser(fromNetwork);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
我正在遵循我的应用程序的 this 指南。
这是 DAO 方法
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertUser(User user);
用户实体(响应正文)
@Entity(tableName = "user_table")
public class User {
@PrimaryKey
@NonNull
private String uid;
@NonNull
@ColumnInfo(name = "password")
private String password;
@SerializedName("first_name")
@ColumnInfo(name = "first_name")
private String firstName;
@SerializedName("last_name")
@ColumnInfo(name = "last_name")
private String lastName;
@ColumnInfo(name = "role")
private int role;
@ColumnInfo(name = "topic")
private String topic;
@ColumnInfo(name = "lab")
private String lab;
//constructor, setters and getters
}
和视图模型
public class UserViewModel extends AndroidViewModel {
private AppRepository mRepository;
public UserViewModel(Application application) {
super(application);
mRepository=new AppRepository(application);
}
public LiveData<User> getUser(String username) {
return mRepository.getUser(username);
}
}
我明白了。
response.body()
交付的 User
在一个字段中有一个错误的值,因此 Room 正在启动一个错误,因为它无法在数据库中插入错误的值。
我试图在 Room 中插入 response.body()
,这是一个 User
,但我得到
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 23714 (AsyncTask #2)
它显示在 RecyclerView
中,但是当我尝试缓存它时我的应用程序崩溃了。
这是代码
private void refreshUser() {
executor.execute(new Runnable() {
@Override
public void run() {
try {
retrofit2.Response<User> response=webservice.getUser("1").execute();
User fromNetwork=response.body();
mUserDao.insertUser(fromNetwork);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
我正在遵循我的应用程序的 this 指南。
这是 DAO 方法
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertUser(User user);
用户实体(响应正文)
@Entity(tableName = "user_table")
public class User {
@PrimaryKey
@NonNull
private String uid;
@NonNull
@ColumnInfo(name = "password")
private String password;
@SerializedName("first_name")
@ColumnInfo(name = "first_name")
private String firstName;
@SerializedName("last_name")
@ColumnInfo(name = "last_name")
private String lastName;
@ColumnInfo(name = "role")
private int role;
@ColumnInfo(name = "topic")
private String topic;
@ColumnInfo(name = "lab")
private String lab;
//constructor, setters and getters
}
和视图模型
public class UserViewModel extends AndroidViewModel {
private AppRepository mRepository;
public UserViewModel(Application application) {
super(application);
mRepository=new AppRepository(application);
}
public LiveData<User> getUser(String username) {
return mRepository.getUser(username);
}
}
我明白了。
response.body()
交付的 User
在一个字段中有一个错误的值,因此 Room 正在启动一个错误,因为它无法在数据库中插入错误的值。