Android-Get network data and store data in a local database

Source: Internet
Author: User

 Public Static Final intDownloaddone = 1; //User Model ArrayArraylist<loginer> loginers =NewArraylist<>(); //handerHandler Downloadloginerhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseDownloadDone:Toast.makeText (Myapplication.getapplication (),"Synchronizing User Data Success", Toast.length_short). Show ();  Break; }            Super. Handlemessage (msg);    }    }; //update Loginer data from the server     Public  voidDownloaddata ()throwsIOException, xmlpullparserexception {NewThread (NewRunnable () {@Override Public voidrun () {String URL= "Http://222.169.11.226:7000/WebServiceAndroidcm.asmx/getloginerInfo"; Myhttp myhttp=Newmyhttp (); Map Map=NewHashMap (); Map.put ("PW", "0535deer"); Try{string String=myhttp.dopost (URL, map); System.out.println (Success:::: "+string); String newstring= String.Replace ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? ><string xmlns=\" http://tempuri.org/\ ">{\" Result\ ": \" 1\ ", \" value\ ":", ""); String laststring= Newstring.replace ("}</string>", "" "); //parsing a stringJsonparser Jsonparser =NewJsonparser (); //convert jsonstring to JaonarrayJsonarray Jsonarray =Jsonparser.parse (laststring). Getasjsonarray (); //GsonGson Gson =NewGson (); //For Loop traversal Josnarray                     for(jsonelement loginerelement:jsonarray) {Loginer Loginer= Gson.fromjson (Loginerelement, Loginer.class);                        Loginers.add (Loginer); System.out.println ("Size is" +loginers.size ()); }                    //Get DAO objectLoginerdao =myapplication.getapplication (). Getdaosession (). Getloginerdao (); //Create a new Message objectMessage msg =NewMessage (); //inserting data into a database with a for loop                     for(inti = 0; I < loginers.size (); i++) {System.out.println ("Finally Found you:" +Loginers.get (i). Getloginer_code ()); Longresult =Loginerdao.insertorreplace (Loginers.get (i)); System.out.println ("The result is:" +result); //if all replacements or inserts succeed, the update user data pops up successfully                        if(Result = = Loginers.get (Loginers.size ()-1). getloginer_id ()) {//send a synchronous completed message to the messagesMsg.what =Downloaddone;                            Downloadloginerhandler.sendmessage (msg); System.out.println ("To the last one."); }                    }                } Catch(Exception e) {e.printstacktrace ();    }}). Start (); }

Call:

// start updating data                for loginers data Try {                    downloaddata ();                 Catch (IOException e) {                    e.printstacktrace ();                 Catch (xmlpullparserexception e) {                    e.printstacktrace ();                }

:

Android-Get network data and store data in a local database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.