I'm following this guide Tutorial to create a fetch database function within my android application
Here is the similar issue, but for him, problem is the url is not a file, its a directory.
When my application running, it need auto download a database file from my local server which directory is 192.168.1.150/db/wifin.db
, i tested from my browser download file correctly, but my application only can get a empty database with a correct name.
Affection: Because of this problem, makes my program a null exception on null sqlite table
Here is my code.
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();
}
here is the cat log
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