[英]Insert Retrofit response body in 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();
}
}
});
}
我正在为我的应用程序遵循此指南。
这是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无法启动错误,因为它无法在数据库中插入错误的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.