我正在学习制作android应用程序。我的应用程序的目标是将来自 URL 的 XML 文件的解析数据存储到 SQLite 数据库中。我已经成功完成了解析过程,但我想将数据从 XML 存储到 SQLite,这样我的应用程序就可以在 Internet 访问中断时打开 XML 数据。这是我项目中的代码
MainActivity.java
public class MainActivity extends ListActivity {
static final String URL = "http://data.bmkg.go.id/propinsi_15_2.xml";
static final String KEY_ITEM = "Cuaca";
static final String KEY_ID = "Isi";
static final String KEY_ROW = "Row";
static final String KEY_KOTA = "Kota";
static final String KEY_LINTANG = "Lintang";
static final String KEY_BUJUR = "Bujur";
static final String KEY_CUACA = "Cuaca";
static final String KEY_SUHUMIN = "SuhuMin";
static final String KEY_SUHUMAX = "SuhuMax";
static final String KEY_KELEMBAPANMIN = "KelembapanMin";
static final String KEY_KELEMBAPANMAX = "KelembapanMax";
static final String KEY_KECEPATANANGIN = "KecepatanAngin";
static final String KEY_ARAHANGIN = "ArahAngin";
private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AmbilData().execute();
}
class AmbilData extends AsyncTask<String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading, Mohon Menunggu Beberapa saat...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground (String... args){
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ROW);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_KOTA, parser.getValue(e, KEY_KOTA));
map.put(KEY_LINTANG, parser.getValue(e, KEY_LINTANG));
map.put(KEY_BUJUR, parser.getValue(e, KEY_BUJUR));
map.put(KEY_CUACA, parser.getValue(e, KEY_CUACA));
map.put(KEY_SUHUMIN, parser.getValue(e, KEY_SUHUMIN));
map.put(KEY_SUHUMAX, parser.getValue(e, KEY_SUHUMAX));
map.put(KEY_KELEMBAPANMIN, parser.getValue(e, KEY_KELEMBAPANMIN));
map.put(KEY_KELEMBAPANMAX, parser.getValue(e, KEY_KELEMBAPANMAX));
map.put(KEY_KECEPATANANGIN, parser.getValue(e, KEY_KECEPATANANGIN));
map.put(KEY_ARAHANGIN, parser.getValue(e, KEY_ARAHANGIN));
menuItems.add(map);
}
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(MainActivity.this, menuItems, R.layout.list_kota, new String[] {KEY_KOTA, KEY_CUACA, KEY_LINTANG, KEY_BUJUR, KEY_SUHUMIN, KEY_SUHUMAX, KEY_KELEMBAPANMIN, KEY_KELEMBAPANMAX, KEY_KECEPATANANGIN, KEY_ARAHANGIN}, new int[] {R.id.kota, R.id.cuaca, R.id.lintang, R.id.bujur, R.id.suhumin, R.id.suhumax, R.id.kelembapanmin, R.id.kelembapanmax, R.id.kecepatanangin, R.id.arahangin});
setListAdapter(adapter);
}
});
}
}
}
我一直在搜索并从互联网上找到了许多教程,但我不知道在我的应用程序中使用哪种最佳方法。