簡體   English   中英

將 For-Each-Loop 中 Retrofit2 調用的服務器響應保存到文件、數據庫或新列表 / ArrayList

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM