2017-10-13 93 views
-2

我在PHP webservice下面调用并获取xml响应并解析并将其设置为hashmap.now我希望在离线模式下保存服务器响应。我可以保存离线模式下的数据.how plz给我答案。在sqlite数据库中保存服务器响应(离线)

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

     } 
    } 
} 
+0

这个链接可以帮助你[将数据保存在SQL数据库(https://developer.android.com/training/得到基础/数据存储/ databases.html) – Jerrol

回答

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; 

} 
相关问题