我是制作android应用程序的新手。我想问我将如何执行以下提到的任务。
好吧,我有一个在我办公室的服务器上运行的离线网站。应用程序将有一个表单,其<select>
选项是从我们服务器中的数据库中获取的。
即使我制作表格,我将如何获得所有这些选项?
即使我在 android 数据库中导入完整的数据库,在服务器数据库上的字段更新上,android 应用程序数据库上的字段将如何更新?
另外,我将如何将插入的字段与服务器数据库同步?
也许您可以使用这样的类访问它。
包 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 });
我认为 Android 无法连接到数据库服务器。您需要创建一个简单的 Web 服务来传递请求并返回响应。
这显示了如何设置 Web 服务 JSON (MySql+Php) + 如何在 Android 中使用它。
希望有帮助。