2014-02-26 25 views
0

我試圖從服務器下載數據庫,然後在我的應用程序中使用它。我最初的情況是,我的資產文件夾中有一個數據庫,當用戶點擊一個按鈕時,我試圖從服務器上下載整個數據庫。如何下載數據庫並將其集成到應用程序中

我想我必須使用onUpgrade方法,但我不知道如何下載數據庫,然後使用它或將其加載到資源文件夾中。

+0

資產文件夾只可讀不可寫。 – Yogendra

回答

0

您需要將問題解決爲較小的問題。

  • 確保服務器端能夠爲您提供一個網絡服務,您將使用該服務進行實際下載。
  • 這將是理想的,如果你的服務器會壓縮你的sqlite數據庫,以使您儘量減少流量。
  • 在設備上下載了數據庫之後,如果採取了這種方法,則必須將其解壓縮。
  • 當你開始下載和解壓縮數據庫時,只需要連接到sqlite數據庫,Android在這方面提供了API。

當然,以上所有需要在多線程環境中完成,您還可以考慮使用Android服務。

這是一個從服務器上下載文件的例子,它應該有助於您獲得一個想法,它不是非常通用,它適用於我的環境。

private void handleSelectedItemDownload(final String downloadItem) { 
    try { 
     int bufferLength; 
     long downloadedSize = 0; 
     long downloadProgressBytes = 0; 

     final byte[] buffer = new byte[12 * 163840]; 
     this.url = new URL(String.format(AppConstants.SERVICE_BASE_URL + AppConstants.MEDIA_IMAGE_REQUEST_URL_PATH + downloadItem)); 
     InputStream inputStream = this.getStream(downloadedSize); 
     downloadedSize = this.chunkSize; 

     if (downloadedSize > 0) { 
      this.applicationFolder = downloadItem.equals("pictures") ? Utils.getThumbNailsFolder() : Utils.getApplicationFolder(); 
      final File mediaFolder = new File(this.applicationFolder); 
      mediaFolder.mkdirs(); 

      final File file = new File(mediaFolder, String.format(File.separator + downloadItem + ".zip")); 
      final FileOutputStream fileOutput = new FileOutputStream(file); 
      this.actionStatusProgressBar.setProgress(0); 

      final Message downloadStartMessage = new Message(); 
      downloadStartMessage.what = ACTION_STARTED; 
      this.actionMessageHandler.sendMessage(downloadStartMessage); 

      int progressBarMax = (int) (this.totalDownloadSize/1024); 
      this.actionStatusProgressBar.setMax(progressBarMax); 


      while (chunkSize != 0) { 
       while ((bufferLength = inputStream.read(buffer)) > 0) { 
        fileOutput.write(buffer, 0, bufferLength); 
        downloadProgressBytes += bufferLength; 

        final Message currentProgressMessage = new Message(); 
        currentProgressMessage.arg1 = (int) (downloadProgressBytes/1024); 
        currentProgressMessage.what = ACTION_PROGRESS_VALUE; 
        this.actionMessageHandler.sendMessage(currentProgressMessage); 
       } 

       inputStream.close(); 
       this.urlConnection.disconnect(); 

       inputStream = this.getStream(downloadedSize); 
       downloadedSize += chunkSize; 
      } 
      fileOutput.close(); 
     } 

     this.urlConnection.disconnect(); 
     this.totalDownloadSize = 0; 

    } catch (MalformedURLException e) { 
     Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString()); 
     this.errorEncountered = true; 
     this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED; 
     this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED); 
    } catch (IOException e) { 
     Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString()); 
     this.errorEncountered = true; 
     this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED; 
     this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED); 
    } catch (Exception e) { 
     Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString()); 
     this.errorEncountered = true; 
     this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED; 
     this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED); 
    } 
} 

private InputStream getStream(final long downloadedSize) throws IOException { 
    this.urlConnection = (HttpURLConnection) this.url.openConnection(); 

    this.urlConnection.setRequestMethod("GET"); 
    this.urlConnection.addRequestProperty("DOWNLOADED_SIZE", String.valueOf(downloadedSize)); 

    final InputStream inputStream = this.urlConnection.getInputStream(); 
    this.chunkSize = this.urlConnection.getContentLength(); 
    if (!Utils.stringIsNullOrEmpty(this.urlConnection.getHeaderField("TOTAL_SIZE"))) { 
     if (this.totalDownloadSize == 0) { 
      this.totalDownloadSize = Long.parseLong(this.urlConnection.getHeaderField("TOTAL_SIZE")); 
     } 
    } 
    return inputStream; 
} 

之後,當你已經下載了你的數據庫文件,你可以創建一個SQLiteDatabase對象:

final File dbFile = new File(filePath); 

    if (dbFile.exists()) { 
     this.localDatabase = SQLiteDatabase.openDatabase(filePath, null, SQLiteDatabase.OPEN_READWRITE); 
     this.localDatabase.setLocale(Locale.getDefault()); 
    } 

我仍然堅持認爲,你應該也給拉上一個想法,你減少把流量和花在網絡上的時間放在一起。

+0

是的,我得到了一個服務器和everthing。我不需要壓縮我的數據庫,因爲它的大小約爲2 MB。 你可以給我一個教程什麼的。請 – schocka94

+0

我已經添加了一些代碼,查看一下,它應該可以幫助你獲得一個想法,否則我可能會在下午給你一些進一步的解釋。 –

+0

是否有可能以更簡單的方式下載整個數據庫? – schocka94

相關問題