2013-07-08 193 views
-1

我是制作android应用程序的新手。我想问我如何去做下面提到的任务。使用离线服务器数据库的Android应用程序

那么我有一个离线网站,跑在我的办公室的服务器上。应用程序将有一个表格,其<select>选项是从我们的服务器中的数据库中获取的。

即使我制作表格,我将如何获得所有这些选项?

即使我在android数据库中导入完整的数据库,在服务器数据库上的字段更新上,android应用程序数据库上的字段将如何更新?

另外,我将如何同步插入的字段与服务器数据库?

回答

0

也许你可以像这样的类来访问它。

包com.yourpackage

import java.io.BufferedInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.os.AsyncTask; 
import android.util.Log; 


     public class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
      @Override 
      protected String doInBackground(String... urls) { 
       String response = ""; 
       String DB_NAME = "sqlbeerlist.sqlite"; 

      try { 
       URL url = new URL(urls[0]); 
       URLConnection connection = url.openConnection(); 
       connection.connect(); 
       // this will be useful so that you can show a typical 0-100% progress bar 
       int fileLength = connection.getContentLength(); 

       // download the file 
       InputStream input = new BufferedInputStream(url.openStream()); 
       OutputStream output = new FileOutputStream("/sdcard/"+DB_NAME); 

       byte data[] = new byte[1024]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        //publishProgress((int) (total * 100/fileLength)); 
        output.write(data, 0, count); 
       } 

       output.flush(); 
       output.close(); 
       input.close(); 

     } catch (IOException e) { 
       Log.d("ImageManager", "Error: " + e); 
     } 
       return response; 
      } 


     } 

执行任务使用

String sUrl = "URL TO YOUR FILE"; 
    DownloadWebPageTask task = new DownloadWebPageTask(); 
      task.execute(new String[] { sUrl }); 
0

我认为Android无法连接到数据库服务器。您需要创建一个简单的Web服务来传递请求并返回响应。

This显示了如何设置Web服务JSON(MySql + Php)+如何在Android上使用它。

希望有所帮助。

+0

的Android可以连接到直接使用JDBC的数据库服务器。我在一个定制的嵌入式应用程序上以这种方式使用FireBird数据库。不过,我也建议按照你的建议创建和使用webservice。 – lopisan

相关问题