将 For-Each-Loop 中 Retrofit2 调用的服务器响应保存到文件、数据库或新列表/ArrayList
Save Server Response from Retrofit2 Call in For-Each-Loop to file, database or new List / ArrayList
我正在开发一个 Android 应用程序并收到服务器响应,我想将该响应存储在数据库中或存储在 ArrayList 或列表中,以便遍历值并比较值到扫描的字符串;记录的输出是我需要并想要保存的数据;不幸的是,我的 Java 技能不太好,所以我真的不知道如何保存这些数据,例如另一个 List 或 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;
}
}
我想稍后存储在数据库中的输出,但首先存储在文件或数组列表或列表中,在此处:
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));
这正是我需要的数据,因此我真的不知道如何单独“放置”数据;我有一个数据库,但首先我想将它存储在文件中或 ArrayList / List 中,具体取决于什么更有意义。
我该怎么做?
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 将包含各自的值。
我正在开发一个 Android 应用程序并收到服务器响应,我想将该响应存储在数据库中或存储在 ArrayList 或列表中,以便遍历值并比较值到扫描的字符串;记录的输出是我需要并想要保存的数据;不幸的是,我的 Java 技能不太好,所以我真的不知道如何保存这些数据,例如另一个 List 或 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;
}
}
我想稍后存储在数据库中的输出,但首先存储在文件或数组列表或列表中,在此处:
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));
这正是我需要的数据,因此我真的不知道如何单独“放置”数据;我有一个数据库,但首先我想将它存储在文件中或 ArrayList / List 中,具体取决于什么更有意义。
我该怎么做? 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 将包含各自的值。