2013-10-13 116 views
0

一個空數據庫,我按照本指南Tutorial我的Android應用程序中創建一個數據庫獲取功能安卓:得到本地服務器

這裏是類似issue,但對他來說,問題是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 

回答

0

將其更改爲URL url = new URL("http://192.168.1.150/db/wifin.db"); ,去刪除 connection.connect(); 將工作