android 云端数据库更新到本地
生活随笔
收集整理的這篇文章主要介紹了
android 云端数据库更新到本地
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面貼代碼
?
請(qǐng)求接口對(duì)比時(shí)間
OkHttpUtils.get().url(StringUtil.DANCE_DB_VERSION_PATH).build().execute(new StringCallback() {@Overridepublic void onError(Call call, Response response, Exception e, int id) {}@Overridepublic 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() {@Overridepublic void run() {downloadNewDb(currentDbDateStr, dbUpdateTime);}}).start();}} catch (ParseException e) {}}}} catch (JSONException e) {e.printStackTrace();}}});下載數(shù)據(jù)庫(kù)
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時(shí)間
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 "";}}寫(xiě)入本地TXT時(shí)間
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) {}}數(shù)據(jù)查詢和插入
private void danceUpdate(String currentDbDateStr, String dbUpdateTime) {List<Dances> updateDaceList = dbDancesUpdateUtil.getUpdateDance(currentDbDateStr);dbDancesUpdateUtil.insertUpdatedance(updateDaceList);writeSDFile("mnt/sdcard/dbDacesUpdateDataTime.txt", dbUpdateTime);}?
總結(jié)
以上是生活随笔為你收集整理的android 云端数据库更新到本地的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: windows引导过程以及多系统引导原理
- 下一篇: linux cmake编译源码,linu