- 服务器端存放一个sqlite数据库
- 服务器端存放一个json文件或者写一个接口(更新时间)
- 本地sdcard存放一个txt记录更新时间
- app启动首先获取服务器端json文件中的更新时间,然后读取本地txt中更新时间
- 若本地时间早于服务器端时间,则下载服务器端数据库
- 下载完成连接并查询将数据插入本地数据库;
- 数据更新之后将更新时间写入本地txt
下面贴代码
请求接口对比时间
OkHttpUtils.get()
.url(StringUtil.DANCE_DB_VERSION_PATH)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Response response, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response);
final String dbUpdateTime = jsonObject.optString("update_time");
Log.d("[dbUpdateTime]", "onResponse: " + dbUpdateTime);
if (!dbUpdateTime.equals("")) {
final String currentDbDateStr = readSDFile("mnt/sdcard/dbDacesUpdateDataTime.txt");
if (currentDbDateStr != null && !currentDbDateStr.equals("")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date currentDate = sdf.parse(currentDbDateStr);
Date networkDate = sdf.parse(dbUpdateTime);
if (networkDate.after(currentDate)) {
new Thread(new Runnable() {
@Override
public void run() {
downloadNewDb(currentDbDateStr, dbUpdateTime);
}
}).start();
}
} catch (ParseException e) {
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
下载数据库
private void downloadNewDb(String currentDbDateStr, String dbUpdateTime) {
String downloadStr = StringUtil.DANCES_INFO_PATH;
String newSongPath = StringUtil.CURRENT_NEW_DANCES_PATH;
File nFile = new File(newSongPath);
if (nFile.exists()) {
nFile.delete();
}
HttpURLConnection httpURLConnection = null;
RandomAccessFile raf = null;
InputStream is = null;
try {
URL url = new URL(downloadStr);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("GET");
int code = httpURLConnection.getResponseCode();
if (code == 200) {
int length = httpURLConnection.getContentLength();
is = httpURLConnection.getInputStream();
raf = new RandomAccessFile(nFile, "rw");
raf.seek(0);
int len = 0;
long totalSize = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
totalSize = totalSize + len;
raf.write(buffer, 0, len);
}
danceUpdate(currentDbDateStr, dbUpdateTime);
}
} catch (Exception e) {
Log.d("[Exception]", "downloadNewDb: " + e.toString());
} finally {
try {
if (is != null) {
is.close();
}
if (raf != null) {
raf.close();
}
} catch (Exception e2) {
}
}
}
读取本地TXT时间
private String readSDFile(String fileName) {
try {
File file = new File(fileName);
if (!file.exists()) {
writeSDFile(fileName, "2018-10-01");
return "2018-10-01";
} else {
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte[] buffer = new byte[length];
fis.read(buffer);
String res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
写入本地TXT时间
private void writeSDFile(String fileName, String write_str) {
try {
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
} catch (Exception e) {
}
}
数据查询和插入
private void danceUpdate(String currentDbDateStr, String dbUpdateTime) {
List<Dances> updateDaceList = dbDancesUpdateUtil.getUpdateDance(currentDbDateStr);
dbDancesUpdateUtil.insertUpdatedance(updateDaceList);
writeSDFile("mnt/sdcard/dbDacesUpdateDataTime.txt", dbUpdateTime);
}