将数据保存到“doInBackground”中的SharedPreferences

时间:2014-08-22 22:57:42

标签: java android android-asynctask sharedpreferences

我想将一些数据保存到AsyncTask内的SharedPreferences中,我已经google了,并且在这里跟着其他一些答案,但我似乎无法让它正常工作。

 public class NtmLoadingTask extends AsyncTask<Void, Void, Void> {


    ArrayList<String> arr_dataNtmTitles = new ArrayList<String>();
    ArrayList<String> arr_dataNtmDates = new ArrayList<String>();
    ArrayList<String> arr_dataNtmContents = new ArrayList<String>();

 public NtmLoadingTask(Context applicationContext) {
}

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... params) {

        Document docNTM;
        try {
            Connection.Response response = Jsoup.connect("http://omitted")
                    .timeout(10000)
                    .ignoreHttpErrors(true)
                    .execute();

            int statusCode = response.statusCode();
            if (statusCode == 200) {

                docNTM = Jsoup.connect("http://omitted").timeout(10000).get();

                Elements tableRows;
                tableRows = docNTM.select("table[title= TABLE]");

                //Get the Notices to Mariners Amount
                Element ntmNumber = tableRows.select("td:eq(0)").last();
                String ntmAmt = ntmNumber.text();

                //In-case Data does not exist...
                if (tableRows != null) {//Exists...

                    //Convert Ntm Number to int for Gathering the Ntm List
                    int ntmInt = Integer.parseInt(ntmAmt);
                    for (int i = 0; i < ntmInt; i++) {

                        //Get Ntm Titles
                        Elements titles = tableRows.select("td:eq(1)");
                        String ntmTitle = titles.get(i).text() + "\n";
                        arr_dataNtmTitles.add(ntmTitle);


                        //Get Ntm Dates
                        Elements dates = tableRows.select("td:eq(2)");
                        String ntmDates = dates.get(i).text() + "\n";
                        arr_dataNtmDates.add(ntmDates);

                        //Get Ntm Content
                        Elements contents = tableRows.select("td:eq(3)");
                        String ntmContent = contents.get(i).text() + "\n";
                        arr_dataNtmContents.add(ntmContent);


*****THIS IS WHERE I WOULD LIKE TO SAVE THE DATA SO AS ONCE THIS TASK HAS COMPLETED 
             I CAN USE IT IN ANOTHER ACTIVITY**********************


                     };

                } else {
                    System.out.println("No Data : " + statusCode );
                }
            } else {
                System.out.println("Received error code " + statusCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Received timeout error code);
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void params){

    System.out.println("NTM Content Captured and Saved");

       }
 }

我在加载应用程序时在另一个活动中使用Sharedpreferences,我理解如何在正常活动中使用它,但在尝试保存任何内容时会抛出一大堆错误或空指针。

那么我应该访问已经在调用asyncTask的加载活动中创建的共享首选项文件,还是在doinbackground中创建一个新的共享首选项文件?

谢谢

我在加载活动中创建的共享首选项:

//Created
public static final String APP_DATA = "AppData";
SharedPreferences DATA;
SharedPreferences.Editor Editor;

//Usage
DATA = getSharedPreferences(APP_DATA, MODE_PRIVATE);
Editor = DATA.edit();

Editor.clear();
            Editor.putString("HomePort", selectedItem);//save name
            Editor.apply();
            Editor.commit();

1 个答案:

答案 0 :(得分:0)

我相信我没有正确地传递上下文正确的方法对我来说如下:

public class NtmLoadingTask extends AsyncTask<Void, Void, Void> {

public static final String APP_DATA = "AppData";
SharedPreferences DATA;
SharedPreferences.Editor Editor;

ArrayList<String> arr_dataNtmTitles = new ArrayList<String>();
ArrayList<String> arr_dataNtmDates = new ArrayList<String>();
ArrayList<String> arr_dataNtmContents = new ArrayList<String>();

public NtmLoadingTask(Context context) {

以前是我出错了我没有正确传递上下文

    DATA = context.getSharedPreferences(APP_DATA, Context.MODE_PRIVATE);
    Editor = DATA.edit();

}

@Override
protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected Void doInBackground(Void... params) {

我现在也在onPostExecute

中保存数据
相关问题