将ArrayList中已解析的XML保存到本地

时间:2017-01-19 03:22:53

标签: android xml web-services listview arraylist

我正在调用一个返回XML的Web服务,我已经使用XMLPullParser解析了响应。然后我创建一个ArrayList并将其传递给我的listView。我想要做的是在本地保存,因为Web服务非常慢。我不确定我是否可以使用SharedPreferences,因为响应是字符串。我最常遇到的问题是人们正在传递一个List或一个ArrayList,但是我的数组列表与我的bean类相关联,用于使用XMLPullParser进行解析。我已经尝试过使用LocalDB,但是我遇到了将ArrayList传递给它的麻烦。

发布我的Web服务调用,因为我想在将数据传递给ListView之前保存数据,因为我需要从中获取第一条记录并填充1列表视图,然后我要在第二个列表中显示其余部分。如果我可以保存这些数据,那么这样做会更容易。

public void getXMLData() {
        OkHttpClient client = getUnsafeOkHttpClient();
        Request request = new Request.Builder()
                .url(getString(R.string.API_FULL_URL))
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String responseData = response.body().string();
                final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
                XMLPullParserHandler parserHandler = new XMLPullParserHandler();
                ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
                Log.i("AfterParse ", employees.toString());
                mEmployees.clear();
                mEmployees.addAll(employees);

                //want to save the data here if possible

                //tell adapter on the UI thread its data changed
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTopListViewAdapter.notifyDataSetChanged();
                        mBottomListViewAdapter.notifyDataSetChanged();
                        mMangerList.setVisibility(View.VISIBLE);
                        directReportListView.setVisibility(View.VISIBLE);
                        mProgressBar.setVisibility(View.GONE);
                    }
                });
            }
        });
    }

ArrayList的响应为 [Employee [Associate = null,Location_name = null,Preferred_first_name = null,PictureURL = null]]

在将Serializable实现到Employee类之后,尝试将员工添加到共享首选项。我收到一个错误,我需要传递一个Set,我正在传递Set我不确定我是否可以将其转换为Set以便能够使用。

SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(getString(R.string.one_team_shared), 0);
                SharedPreferences.Editor editor = sharedPref.edit();

                Set<Employee> set = new HashSet<>();
                set.addAll(employees);
                editor.putStringSet("myKey", set);
                editor.commit();

1 个答案:

答案 0 :(得分:1)

您可以将Employee类序列化并存储在SharedPreferences中(作为XML或JSON)

或者您可以尝试这样的事情:

void saveToCache(String responseBody) {
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(getString(R.string.one_team_shared), 0);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString("cache", responseBody).commit();
}

//return empty list if cache is not available
void List<Employee> getFromCache() {
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(getString(R.string.one_team_shared), 0);

    String cachedResponse = sharedPref.getString("cache", "");
    if(cachedResponse.equals("") {
        return Collections.emptyList();
    }

    final InputStream stream = new ByteArrayInputStream(cachedResponse.getBytes());
    XMLPullParserHandler parserHandler = new XMLPullParserHandler();
    List<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
    return employees;
}

从后端收到响应后调用saveToCache(String),并在需要缓存数据时随时调用getFromCache()。这是我能想到的最简单的解决方案。