在sqlite数据库中保存服务器响应(离线)

时间:2017-10-13 13:27:23

标签: java android sqlite

我在php webservice下面调用并获取xml响应并解析共振并将其设置为hashmap.now我想在离线模式下保存服务器响应。我可以在离线模式下保存数据。请给我答案。

 @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        try {

            if (response.length() > 0) {
                int code = Integer.parseInt(XMLManualParser.getTagValue(Constant.TAG_CODE, response));
                if (code == 1) {
                    spinner.clear();
                    ArrayList<String> eventlist = XMLManualParser.getMultipleTagList(Constant.TAG_LIST, response);
                    for (int i = 0; i < eventlist.size(); ++i) {
                        String responseContent = eventlist.get(i);
                        HashMap<String, String> hashMap = new HashMap<String, String>();
                        String title = XMLManualParser.getTagValue(Constant.title, responseContent);
                        hashMap.put("title", title);
                        hashMap.put("id", XMLManualParser.getTagValue(Constant.id, responseContent));
                        hashMap.put("url", XMLManualParser.getTagValue(Constant.url, responseContent));
                        hashMap.put("balanceurl", XMLManualParser.getTagValue(Constant.balanceurl, responseContent));
                        spinner.add(hashMap);
                    }
                } else {
                    Toast.makeText(SettingsEditActivity.this, "no data found", Toast.LENGTH_SHORT).show();
                }
                CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(SettingsEditActivity.this, spinner);
                settingsBinding.splist.setAdapter(customSpinnerAdapter);
                settingsBinding.splist.setOnItemSelectedListener(new OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        item = parent.getItemAtPosition(position).toString();
                        // Toast.makeText(parent.getContext(), "Android Custom Spinner Example Output..." + item, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将回复保存到文件中并离线

public static void saveDataSingleItemDetails(Context context,String file, MainListModel data) {

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = context.openFileOutput(file, MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        return;
    }
    ObjectOutputStream objectOutputStream = null;
    try {
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
    } catch (IOException | NullPointerException e) {
        e.printStackTrace();
    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.writeObject(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }


}

public static MainListModel getSinglItemDetails(Context context,String file) {

    MainListModel items = null;
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = context.openFileInput(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return items;
    } catch (NullPointerException e) {
        return items;
    }
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(fileInputStream);
    } catch (IOException | NullPointerException e) {
        e.printStackTrace();
        return items;
    }
    try {
        items = (MainListModel) objectInputStream.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        objectInputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return items;

}