日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

android 云端数据库更新到本地

發(fā)布時(shí)間:2023/12/20 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 云端数据库更新到本地 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • 服務(wù)器端存放一個(gè)sqlite數(shù)據(jù)庫(kù)
  • 服務(wù)器端存放一個(gè)json文件或者寫(xiě)一個(gè)接口(更新時(shí)間)
  • 本地sdcard存放一個(gè)txt記錄更新時(shí)間
  • app啟動(dòng)首先獲取服務(wù)器端json文件中的更新時(shí)間,然后讀取本地txt中更新時(shí)間
  • 若本地時(shí)間早于服務(wù)器端時(shí)間,則下載服務(wù)器端數(shù)據(jù)庫(kù)
  • 下載完成連接并查詢將數(shù)據(jù)插入本地?cái)?shù)據(jù)庫(kù);
  • 數(shù)據(jù)更新之后將更新時(shí)間寫(xiě)入本地txt
  • 下面貼代碼

    ?

    請(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)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。