[英]Save Server Response from Retrofit2 Call in For-Each-Loop to file, database or new List / ArrayList
我正在开发 Android 应用程序并接收服务器响应,我想将其存储在数据库中或存储在 ArrayList 或列表中,以便遍历值并将值与扫描的字符串进行比较; 记录的 output 是我需要并想要保存的数据; 不幸的是,我的 Java 技能不是很好,所以我真的不知道如何将这些数据保存在另一个列表或 ArrayList 中。 我需要的数据在那里,因此我真的不知道如何存储它......
这是 API 调用:
public static void writeItemsToDatabase(Context mContext, String basic) {
//creating the itemApi interface
ItemApi itemApi = retrofit.create(ItemApi.class);
//making the call object
Call<List<Item>> call = itemApi.checkItems(basic);
call.enqueue(new Callback<List<Item>>() {
@Override
public void onResponse(@NonNull Call<List<Item>> call,
@NonNull Response<List<Item>> response) {
if (response.isSuccess()) {
List<Item> itemList;
itemList = response.body();
int dataSize = response.body().size();
Log.d(TAGGG, String.valueOf(dataSize));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));
class DownloadTask extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
// Do something that takes a long time, for example:
try (DatabaseHandler erpDevelopment = new DatabaseHandler((XXXApp)
mContext.getApplicationContext())) {
itemList.stream().limit(4600).forEach(item -> {
erpDevelopment.addItem(item);
erpDevelopment.close();
});
}
// Call this to update your progress
return "this string is passed to onPostExecute";
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
}
}
new DownloadTask().execute();
}
}
@Override
public void onFailure(Call<List<Item>> call, Throwable t) {}
});
return;
}
这是项目 Class:
package com.example.xxx;
import com.google.gson.annotations.SerializedName;
public class Item {
@SerializedName("no")
private String no;
@SerializedName("ean")
private String ean;
@SerializedName("name")
private String name;
@SerializedName("itemgroupname")
private String itemgroupname;
@SerializedName("type")
private String type;
@SerializedName("destruction")
private Boolean destruction;
@SerializedName("archived")
private Boolean archived;
public Item(String no, String ean, String name, String type, String itemgroupname, Boolean destruction,
Boolean archived) {
this.no = no;
this.name = name;
this.type = type;
this.ean = ean;
this.itemgroupname = itemgroupname;
this.destruction = destruction;
this.archived = archived;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getEan() {
return ean;
}
public void setEan(String ean) {
this.ean = ean;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getItemgroupname() {
return itemgroupname;
}
public void setItemgroupname (String Itemgroupname) {
this.itemgroupname = itemgroupname;
}
public boolean getDestruction() {
return destruction;
}
public void setDestruction (Boolean Destruction ) {
this.destruction = destruction;
}
public boolean getArchived() {
return true;
}
public void setArchived (Boolean Archived ) {
this.archived = archived;
}
}
output 我想稍后存储在数据库中,但首先存储在文件或 ArrayList 或列表中,在这里:
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));
这正是我需要的数据,因此我真的不知道如何单独“放置”数据; 我有一个数据库,但首先我想将它存储在文件中或 ArrayList / 列表中,具体取决于更有意义的内容。
我怎么做? ForEach-Loop 如何分别保存 List.getEan() 和 List.getNo() 中的所有数据? 任何提示或帮助将不胜感激,在此先感谢。
好的...首先声明两个字符串列表。 一个用于 Ean,另一个用于 No。
List<String> EanList = new ArrayList<>();
List<String> NoList = new ArrayList<>();
像这样添加它们:
public static void writeItemsToDatabase(Context mContext, String basic)
{
List<String> EanList = new ArrayList<>();
List<String> NoList = new ArrayList<>();
................
}
然后在响应成功时执行以下操作:
if(response.isSuccess())
{
int dataSize = itemList.size();
for(int i=0; i<dataSize; i++)
{
EanList.add(itemList.get(i).getEan());
NoList.add(itemList.get(i).getNo());
}
}
在这里,您基本上只需将值复制到另外两个字符串列表中。 在 for 循环运行后,您的 EanList 和 NoList 将包含各自的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.