我正在按照本指南教程在我的 android 应用程序中创建获取数据库函数
这是类似的问题,但对他来说,问题是 url 不是文件,而是目录。
当我的应用程序运行时,它需要从我的本地服务器自动下载一个数据库文件,我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();
}
这是猫日志
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