Gson解析后的数据存到本地数据库 耗时的问题

最近一直在做数据同步 从接口那边拿到的数据存在本地数据库的过程中 加载数据的时间一直是个很头疼的问题 下面有两种方法 对比一下 第二种加载数据耗时更少 更为快捷一些

// [start] 字典数据同步
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, 1000);

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("失败");
}
});
}
//[end]

// [start] 字典数据同步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("失败");
}
});
}
//[end]

时间: 2024-08-07 08:39:33

Gson解析后的数据存到本地数据库 耗时的问题的相关文章

Android -- 获取网络数据并将数据存到本地数据库中

public static final int downloadDone = 1; // 用户model数组 ArrayList<Loginer> loginers = new ArrayList<>(); // hander Handler downloadLoginerHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case downlo

SQL从其他服务器数据库导入数据到本地数据库中

EXEC sp_dropserver 'ITSV2', 'droplogins' exec sp_addlinkedserver 'ITSV2' , '' , 'SQLOLEDB' , '168.9.123.123' exec sp_addlinkedsrvlogin 'ITSV2' , 'false' , null , 'sa' , 'sa' drop table test2; select top 12 * into test2 from ITSV2.InfoDB.dbo.city sele

Oracle如何通过dataLink复制远程数据库的CLOB\BLOB字段数据到本地数据库

Oracle不支持直接通过Database Link复制远程数据库表的CLOB/BLOB字段数据到本地数据库. 像如下的SQL是不能执行的.(ipop_topic表有一个CLOB的字段) insert into ipop_topicselect * from [email protected] where application_id=1000 但是,我们可以借助全局临时表,先把数据复制到临时表,再从临时表转移到你的目的表. create global temporary table ipop_

Java 读取txt文件后把数据保存到数据库中

需求:有一个很大的txt文件(1,000,000条数据),从txt中读取有用数据库后保存到Oracle数据库中 利用Java实现: 1.读取文件 2.数据库连接后插入到数据库 package com.test.IF.service; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedS

Android开发之Gson解析Json嵌套数据

Gson解析复杂的json数据 在这里介绍解析json数据的另外一种方法就是通过Gson解析,对于解析比较简单的json数据我就不介绍了来一个比较复杂一点的json数据,如下面我们要解析的一个json数据: [java] view plaincopy String json = {"a":"100","b":[{"b1":"b_value1","b2":"b_value2&qu

关于listview中图片切圆,网络请求数据,并Gson解析后得到list

切圆的方法: public Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; l

Gson解析json繁杂数据

碰到json数据.里面格式众多.list+string[]+等等.具体json参数如下: eg:以下为接口参数: "responseData":{ "brandCode": "10000002", "brandName": "健康卡", "channelId": 20001, "channelIdOldPc": "wap-fmall-yztapp"

用GSON解析Json格式数据

GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的libs目录下添加这个文件即可:如果是在Java项目中,则把gson-2.2.4.jar先添加到当前项目的任意一个包中,然后右键点击这个jar包 -> 构建路径 -> 添加至构建路径.这样准备工作就做好了. (一)单条无嵌套Json数据的解析 比如有如下Json数据:{"name"

Python爬虫入门 | 6 将爬回来的数据存到本地

1.用Python语句存储数据 写文件时,我们主要用到 with open() 语句: with open(name,mode,encoding) as file:   file.write()   # 注意,with open() 后面的语句有一个缩进 name:包含文件名称的字符串,比如:'xiaozhu.txt'; mode:决定了打开文件的模式,只读/写入/追加等; encoding:表示我们要写入数据的编码,一般为 utf-8 或者 gbk ; file:表示我们在代码中对文件的命名.