Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. private class DownloadXML extends AsyncTask<Void, Integer, Void> {
  2.  
  3. @Override
  4. protected void onPreExecute() {
  5. super.onPreExecute();
  6. mProgressStatus = 0;
  7.  
  8. }
  9.  
  10. @Override
  11. protected Void doInBackground(Void... params) {
  12.  
  13.  
  14. while(mProgressStatus<100){
  15. try{
  16.  
  17. mProgressStatus++;
  18. publishProgress(mProgressStatus);
  19. Thread.sleep(100);
  20.  
  21. try {
  22. datasource = new DataSource(mContext);
  23. db = datasource.getWritableDatabase();
  24. ContentValues cv = new ContentValues();
  25.  
  26. URL url = new URL("http://123.231.241.53:48100/mobileapplication/AndroidFin.asmx/GetZipCode");
  27. DocumentBuilderFactory dbf = DocumentBuilderFactory
  28. .newInstance();
  29. DocumentBuilder db1 = dbf.newDocumentBuilder();
  30. // Download the XML file
  31. Document doc = db1.parse(new InputSource(url.openStream()));
  32. doc.getDocumentElement().normalize();
  33. // Locate the Tag Name
  34. nodelist = doc.getElementsByTagName("zipcode");
  35.  
  36. for (int i=0;i<nodelist.getLength();i++) {
  37. Node fieldNode = nodelist.item(i);
  38. Log.d("waduh", "GetZipCodeData: "+nodelist.getLength());
  39. NamedNodeMap map = fieldNode.getAttributes();
  40.  
  41. cv.put("KOTA", map.getNamedItem("KOTA").getNodeValue());
  42. cv.put("KECAMATAN", map.getNamedItem("KECAMATAN").getNodeValue());
  43. cv.put("KELURAHAN", map.getNamedItem("KELURAHAN").getNodeValue());
  44. cv.put("Description", map.getNamedItem("Description").getNodeValue());
  45.  
  46. datasource.generateData(db, cv, "zipcode");
  47. cv.clear();
  48. }
  49.  
  50.  
  51. Node fieldNode = nodelist.item(0);
  52. NamedNodeMap map = fieldNode.getAttributes();
  53. lastinsertzipcode = map.getNamedItem("lastinsertzipcode").getNodeValue();
  54.  
  55. Cursor cursor = db.rawQuery("SELECT id FROM " + "control", null);
  56. try {
  57. if(cursor.moveToFirst())
  58. {
  59. cv.put("lastinsertzipcode", lastinsertasset);
  60. datasource.updateData(db,cv,"control",null);
  61. }
  62. else{
  63. cv.put("lastinsertzipcode", lastinsertasset);
  64. datasource.generateData(db,cv,"control");
  65. }
  66. }
  67. finally{
  68. cursor.close();
  69. }
  70.  
  71.  
  72. } catch (Exception e) {
  73. Log.e("Error", e.getMessage());
  74. e.printStackTrace();
  75. }finally{
  76. datasource.close();
  77. db.close();
  78. }
  79.  
  80. }catch(Exception e){
  81. Log.d("Exception", e.toString());
  82. }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. return null;
  91. }
  92.  
  93. @Override
  94. protected void onProgressUpdate(Integer... values) {
  95. super.onProgressUpdate(values);
  96. mProgressDialog.setProgress(mProgressStatus);
  97. }
  98.  
  99. @Override
  100. protected void onPostExecute(Void args) {
  101. mProgressDialog.dismiss();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement