Android:从本地服务器获取一个空数据库
我将按照本指南在android应用程序中创建一个获取数据库函数 这里是类似的,但对他来说,问题是url不是一个文件,而是一个目录 当我的应用程序运行时,它需要从我的本地服务器自动下载一个数据库文件,该服务器的目录是Android:从本地服务器获取一个空数据库,android,sqlite,inputstream,outputstream,Android,Sqlite,Inputstream,Outputstream,我将按照本指南在android应用程序中创建一个获取数据库函数 这里是类似的,但对他来说,问题是url不是一个文件,而是一个目录 当我的应用程序运行时,它需要从我的本地服务器自动下载一个数据库文件,该服务器的目录是192.168.1.150/db/wifin.db,我正确地从浏览器下载文件进行了测试,但我的应用程序只能得到一个具有正确名称的空数据库 影响:由于这个问题,使我的程序成为空sqlite表上的空异常 这是我的代码。 public void createDataBase() throws
192.168.1.150/db/wifin.db
,我正确地从浏览器下载文件进行了测试,但我的应用程序只能得到一个具有正确名称的空数据库
影响:由于这个问题,使我的程序成为空sqlite表上的空异常
这是我的代码。
public void createDataBase() throws IOException{
this.getReadableDatabase();
try{
copyDataBase();}
catch (IOException e){
throw new Error("Error copying database");}
}
private void copyDataBase() throws IOException{
//url of my webserver link point to my db
Thread dx = new Thread() {
public void run(){
try{
URL url = new URL("192.168.1.150/db/wifin.db");
//open a connection
URLConnection connection = url.openConnection();
connection.connect();
//Open your local db as the input stream
InputStream input = new BufferedInputStream(url.openStream());
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();}
catch (Exception e)
{
e.printStackTrace();
Log.i("ERROR ON DOWNLOADING FILES", "ERROR IS" +e);
}
}
};
dx.start();
}
这是cat日志
10-13 22:41:34.527: W/System.err(28533): java.net.MalformedURLException: Protocol not found: 192.168.1.150/db/wifin.db
10-13 22:41:34.527: W/System.err(28533): at java.net.URL.<init>(URL.java:178)
10-13 22:41:34.527: W/System.err(28533): at java.net.URL.<init>(URL.java:127)
10-13 22:41:34.527: W/System.err(28533): at com.example.Wifin.DataBaseHelper$1.run(DataBaseHelper.java:110)
10-13 22:41:34.537: I/ERROR ON DOWNLOADING FILES(28533): ERROR ISjava.net.MalformedURLException: Protocol not found: 192.168.1.150/db/wifin.db
10-13 22:41:34.527:W/System.err(28533):java.net.MalformedURLException:找不到协议:192.168.1.150/db/wifin.db
10-13 22:41:34.527:W/System.err(28533):位于java.net.URL。(URL.java:178)
10-13 22:41:34.527:W/System.err(28533):位于java.net.URL。(URL.java:127)
10-13 22:41:34.527:W/System.err(28533):位于com.example.Wifin.DataBaseHelper$1.run(DataBaseHelper.java:110)
10-13 22:41:34.537:下载文件时发生I/错误(28533):错误为java.net.MalformedURLException:找不到协议:192.168.1.150/db/wifin.db
将其更改为URL=newurl(“http://192.168.1.150/db/wifin.db");代码>
然后转到delete connection.connect();
那会有用的