Gson parsing of data to local database time-consuming problem

Source: Internet
Author: User
Tags diff stub tojson stringbuffer

I've been doing data synchronization. The time to load data in the local database is the same as the data that was taken from the interface. There are two ways to compare the second loading data with less and more quickly.

[Start] Dictionary data synchronization
public static void Getupdatedictionary (Final String clientversion, Final context context) {
TODO auto-generated Method Stub
Diccomplete=false;
hashmap<string, string> paramdictionary = new hashmap<string, string> ();
Paramdictionary.put ("ClientVersion", clientversion);
System.out.println ("=======getupdatedictionary" + clientversion);
Baseprotocol Updateprotocol = new Baseprotocol (paramdictionary);
Final String jsondictionary = Updateprotocol.tojson ();

Paramsdictionary = new Requestparams ();
Paramsdictionary.addquerystringparameter ("detail", Jsondictionary + "&" + Desityutil.random ());

Netutils.getdata (httpmethod.post, Constantvalue.common + Constantvalue.updatedictionary_url, paramsDictionary, new Requestcallback<string> () {
@Override
public void onsuccess (responseinfo<string> responseinfo) {
Gson gsondictionary = new Gson ();
if (Hispitalprotocol.checkresult (Responseinfo)) {
System.out.println ("=======responseinfo" + responseinfo);
Final list<sysdicvo> fromjsondictionary = Gsondictionary.fromjson (Hispitalprotocol.getcontentobj ( Responseinfo). ToString (),
New Typetoken<list<sysdicvo>> () {}.gettype ());
New Thread () {
public void Run () {
try {
Date curdate = new Date (System.currenttimemillis ());

db = Dbmanager.getinstance (new DBHelper (context)). OpenDatabase ();
Db.begintransaction ();
Daodictionary = new Sysdicdaoimpl (db);

int row = 0;
for (int i = 0; i < fromjsondictionary.size (); i++) {
SYSDICVO SYSDICVO = new SYSDICVO ();
Sysdicvo.setid (Fromjsondictionary.get (i). GetId ());
Sysdicvo.setcode (Fromjsondictionary.get (i). GetCode ());
Sysdicvo.setname (Fromjsondictionary.get (i). GetName ());
sysdicvo.setparent_id (Fromjsondictionary.get (i). getparent_id ());
Sysdicvo.setbase_version (Fromjsondictionary.get (i). Getbase_version ());
Sysdicvo.setstatus (Fromjsondictionary.get (i). GetStatus ());
Sysdicvo.setsort (0);

List<sysdicvo> Sysdicvos = daodictionary.findbycondition (new string[] {dbhelper.t_id},
DBHelper.T_ID + "= ? ", new string[] {Sysdicvo.getid ()}, NULL);
if (null! = Sysdicvos && sysdicvos.size () > 0) {
Daodictionary.update (SYSDICVO, Sysdicvo.getid ());
} else {
Daodictionary.insert (SYSDICVO);
}

//row = daodictionary.update (SYSDICVO, Sysdicvo.getid ());
if (row==0) {
//daodictionary.insert (SYSDICVO);
}
}
Db.settransactionsuccessful ();

Date endDate = new Date (System.currenttimemillis ());
Long diff = enddate.gettime ()-curdate.gettime ();

Toast.maketext (context, "=================diff================" + diff, +);

Diccomplete=true;
} catch (Exception e) {
Diccomplete=false;
E.printstacktrace ();
} finally {
if (null! = db)
Db.endtransaction ();
}
}
}.start ();
}
}

@Override
public void OnFailure (httpexception error, String msg) {
SYSTEM.OUT.PRINTLN ("failure");
}
});
}
[end]

[Start] Dictionary data synchronization 1
public static void GetUpdateDictionary1 (Final String clientversion, Final context context) {
TODO auto-generated Method Stub
Diccomplete=false;
hashmap<string, string> paramdictionary = new hashmap<string, string> ();
Paramdictionary.put ("ClientVersion", clientversion);
System.out.println ("=======getupdatedictionary1" + clientversion);
Baseprotocol Updateprotocol = new Baseprotocol (paramdictionary);
Final String jsondictionary = Updateprotocol.tojson ();

Paramsdictionary = new Requestparams ();
Paramsdictionary.addquerystringparameter ("detail", Jsondictionary + "&" + Desityutil.random ());

Netutils.getdata (httpmethod.post, Constantvalue.common + Constantvalue.updatedictionary_url, paramsDictionary, new Requestcallback<string> () {
@Override
public void onsuccess (responseinfo<string> responseinfo) {
Gson gsondictionary = new Gson ();
if (Hispitalprotocol.checkresult (Responseinfo)) {
System.out.println ("=======responseinfo" + responseinfo);
Final list<sysdicvo> fromjsondictionary = Gsondictionary.fromjson (Hispitalprotocol.getcontentobj ( Responseinfo). ToString (), new typetoken<list<sysdicvo>> () {
}.gettype ());

New Thread () {
public void Run () {
try {

Date curdate = new Date (System.currenttimemillis ());

db = Dbmanager.getinstance (new DBHelper (context)). OpenDatabase ();
Db.begintransaction ();
Daodictionary = new Sysdicdaoimpl (db);

list<sysdicvo> Sysdicvos = daodictionary.findbycondition (new string[] {dbhelper.t_id}, NULL, NULL, NULL);
//
Map<string, string> maps = new hashmap<string, string> ();
for (SYSDICVO Sysdicvo:sysdicvos) {
Maps.put (Sysdicvo.getid (), Sysdicvo.getid ());
// }

StringBuffer Sql_insert = new StringBuffer ();
StringBuffer sql_update = new StringBuffer ();

Sql_insert.append ("Replace into Sys_dic (ID, code, name, parent_id, base_version, status) values" +
"(?,?,?,?,?,?) ");

Sql_update.append ("update sys_dic Set id =?, code =?, name =?, parent_id =?, Base_version =?, status =?");

Sqlitestatement Stat_insert = db.compilestatement (sql_insert.tostring ());
Sqlitestatement stat_update = db.compilestatement (sql_update.tostring ());

String id = null;
for (int i = 0; i < fromjsondictionary.size (); i++) {
id = maps.get (fromjsondictionary.get (i). GetId ());
if (Null!=id &&! "". Equals (ID) && id.equals (Fromjsondictionary.get (i). GetId ())) {
Stat_update.bindstring (1,fromjsondictionary.get (i). GetId ());
Stat_update.bindstring (2,fromjsondictionary.get (i). GetCode ());
Stat_update.bindstring (3,fromjsondictionary.get (i). GetName ());
Stat_update.bindstring (4,fromjsondictionary.get (i). getparent_id ()!=null? Fromjsondictionary.get (i). GetParent_ ID (): "");
Stat_update.bindstring (5,fromjsondictionary.get (i). Getbase_version ());
Stat_update.bindstring (6,string.valueof (Fromjsondictionary.get (i). GetStatus ()));
Stat_update.executeinsert ();
} else {
Stat_insert.bindstring (1,fromjsondictionary.get (i). GetId ());
Stat_insert.bindstring (2,fromjsondictionary.get (i). GetCode ());
Stat_insert.bindstring (3,fromjsondictionary.get (i). GetName ());
Stat_insert.bindstring (4,fromjsondictionary.get (i). getparent_id ()!=null? Fromjsondictionary.get (i). getParent_id ():"");
Stat_insert.bindstring (5,fromjsondictionary.get (i). Getbase_version ());
Stat_insert.bindstring (6,string.valueof (Fromjsondictionary.get (i). GetStatus ()));
Stat_insert.executeinsert ();
//}



Long result = Stat.executeinsert ();
if (Result < 0) {
Returnfalse;
// }

}
Db.settransactionsuccessful ();

Date endDate = new Date (System.currenttimemillis ());
Long diff = enddate.gettime ()-curdate.gettime ();

LOG.I ("Log", "=================diff================" + diff);

Diccomplete=true;
} catch (Exception e) {
Diccomplete=false;
E.printstacktrace ();
} finally {
if (null! = db)
Db.endtransaction ();
}
}
}.start ();
}
}

@Override
public void OnFailure (httpexception error, String msg) {
SYSTEM.OUT.PRINTLN ("failure");
}
});
}
[end]

Gson parsing of data to local database time-consuming problem

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.