我将 API 响应作为 JSON 对象获取,并且我正在使用模型类在 RecyclerView 中处理它。我将其作为字符串保存到 Room DB。现在我从数据库中选择插入的数据。但是如何处理选定的字符串数据。两者的类型都有一些问题。在选择我正在获取字符串数据作为响应时,但我想以列表类型获取它以在 Recyclerview 中处理它。
使用改造 api 调用获取响应
RequestInterface apiService =
RetrofitClient.getClient(BASE_URL, token).create(RequestInterface.class);
apiService.getAllDrafts().enqueue(new Callback<RetroData>() {
@Override
public void onResponse(Call<RetroData> call, Response<RetroData> response) {
if (response.isSuccessful())
{
List<JSONResponse> MovieList = response.body().getData();
SaveTask st = new SaveTask();
st.execute();
initRecyclerView(MovieList);
}
else {
Toast.makeText(getContext(), "OUTPUT : " + response.code(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<RetroData> call, Throwable t) {
Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
数据库插入和更新:
class SaveTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
MovieDataJSON task = new MovieDataJSON();
task.setTask(contactList.toString());
//adding to database
if(DatabaseClient.getInstance(getContext()).getAppDatabase().getAllJSON()
.getCount()>0) {
DatabaseClient.getInstance(getContext()).getAppDatabase().getAllJSON().update(task);
}
else
{
DatabaseClient.getInstance(getContext()).getAppDatabase().getAllJSON()
.insert(task);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getContext(), "Saved", Toast.LENGTH_LONG).show();
}
}
从数据库中获取数据
class CheckTask extends AsyncTask<Void, Void, Void> {
int jsoncount;
String response;
@Override
protected Void doInBackground(Void... voids) {
//creating a task
jsoncount= DatabaseClient.getInstance(getContext()).getAppDatabase().getAllJSON().getCount();
if(jsoncount>0){
response= String.valueOf(DatabaseClient.getInstance(getContext()).getAppDatabase().getAllJSON().getALLdata());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(getContext(), "Saved..."+response, Toast.LENGTH_LONG).show();
}
}
此选择数据是 String ,但我想将其转换为 List 格式。