[英]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.