2017-10-13 7 views
-2

私はPHPのwebserviceを呼び出し、xmlレスポンスを取得して解析し、hashmap.nowに設定します。オフラインモードでサーバレスポンスを保存します。私に答えてください。サーバのレスポンスを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

を得ることができます(https://developer.android.com/training/基本/データストレージ/ databases.html) – Jerrol

答えて

0

あなたは[SQLデータベースでのデータの保存]をあなたを助けるかもしれない、このリンクをファイルにあなたの応答を保存し、オフラインで

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; 

} 
関連する問題