我正在按照本指南Tutorial在我的Android应用程序中创建一个fetch数据库函数
这是类似的issue,但对他来说,问题是url不是文件,它是一个目录。
当我的应用程序运行时,它需要从我的本地服务器自动下载一个数据库文件,该目录是192.168.1.150/db/wifin.db
,我正确地从我的浏览器下载文件进行了测试,但我的应用程序只能获得一个名称正确的空数据库
感情:由于这个问题,使我的程序在null 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
答案 0 :(得分:0)
将其更改为URL url = new URL("http://192.168.1.150/db/wifin.db");
然后去删除 connection.connect();
这将有效