简体   繁体   English

将 For-Each-Loop 中 Retrofit2 调用的服务器响应保存到文件、数据库或新列表 / ArrayList

[英]Save Server Response from Retrofit2 Call in For-Each-Loop to file, database or new List / ArrayList

I am working on an Android app and receiving a Server response that I would like to either store in a Database or store in an ArrayList or List, in order to iterate through the values and compare the values to a scanned string;我正在开发 Android 应用程序并接收服务器响应,我想将其存储在数据库中或存储在 ArrayList 或列表中,以便遍历值并将值与扫描的字符串进行比较; the logged output is the data that I need and want to save;记录的 output 是我需要并想要保存的数据; unfortunately, my Java skills are not so good so that I don´t really know how to save this data in eg another List or an ArrayList.不幸的是,我的 Java 技能不是很好,所以我真的不知道如何将这些数据保存在另一个列表或 ArrayList 中。 The data that I need is there, hence I don´t really know how to store it...我需要的数据在那里,因此我真的不知道如何存储它......

This is the API-Call:这是 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;
    }

This is the Item Class:这是项目 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;
    }
}

The output I want to store later maybe in a Database, but firstly in a File or in an ArrayList or List, is here: output 我想稍后存储在数据库中,但首先存储在文件或 ArrayList 或列表中,在这里:

  itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getEan())));
  itemList.forEach(List -> Log.d(TAGGG, String.valueOf(List.getNo())));

it is precisely the data that I need, hence I don´t really know on how to "put" the data separately;这正是我需要的数据,因此我真的不知道如何单独“放置”数据; I have a Database, but in the first place I would like to store it either in a file or in an ArrayList / List, depending on what makes more sense.我有一个数据库,但首先我想将它存储在文件中或 ArrayList / 列表中,具体取决于更有意义的内容。

How do I do that?我怎么做? How would a ForEach-Loop look like that saves all the data from List.getEan() and List.getNo() separately? ForEach-Loop 如何分别保存 List.getEan() 和 List.getNo() 中的所有数据? Any hints or help would be very much appreciated, thanks in advance.任何提示或帮助将不胜感激,在此先感谢。

Ok...first declare two lists of strings.好的...首先声明两个字符串列表。 One for the Ean and the another for the No.一个用于 Ean,另一个用于 No。

List<String> EanList = new ArrayList<>();
List<String> NoList = new ArrayList<>();

Add them like this:像这样添加它们:

public static void writeItemsToDatabase(Context mContext, String basic) 
{
    List<String> EanList = new ArrayList<>();
     List<String> NoList = new ArrayList<>();
................
}

Then on the response being successful do this:然后在响应成功时执行以下操作:

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());
     }
}

Here you just basically need to copy the values into two other lists of strings.在这里,您基本上只需将值复制到另外两个字符串列表中。 After the running of for loop, your EanList and NoList will contain respective values.在 for 循环运行后,您的 EanList 和 NoList 将包含各自的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM