I originally posted this in stackoverflow.com but the question may be too broad. I'm trying to figure out the best way to download and use an SQL database from my server. I have included the code i whipped up but I'm not sure if it's a viable way to accomplish this so peer review would be extremely helpful :)
As it stands now, the database will be downloaded in a separate thread but when UI components are initialized they fail (obviously, as the database doesnt exist while its still being downloaded).
package com.sandbox.databaseone;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
public class DatabaseManager {
private SQLiteDatabase database;
private int currentVersion = 0;
private int nextVersion = 0;
private static String databasePath = "/data/data/com.sandbox.databaseone/databases/";
private static String databaseFile = "dbone.sqlite";
private static String databaseBaseURL = "http://www.redstalker.com/dbone/";
private static String databaseVersionURL = "version.txt";
public DatabaseManager()
{
database = null;
}
public void initialize()
{
DatabaseVersionCheck check = new DatabaseVersionCheck();
String url = databaseBaseURL + databaseVersionURL;
check.execute(url);
}
private void init_database(String path)
{
database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
if(database != null)
{
currentVersion = nextVersion;
}
else
{
nextVersion = 0;
}
}
private class DatabaseVersionCheck extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
try
{
HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while((line = reader.readLine()) != null)
{
builder.append(line);
}
in.close();
reader.close();
entity.consumeContent();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String result)
{
if(result != null)
{
int version = Integer.parseInt(result);
if(version > currentVersion)
{
nextVersion = version;
DownloadDatabase d = new DownloadDatabase();
d.execute();
}
}
}
}
private class DownloadDatabase extends AsyncTask<Void, Void, Boolean>
{
@Override
protected Boolean doInBackground(Void... params) {
boolean result = false;
String url = databaseBaseURL + databaseFile;
String path = databasePath + databaseFile;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try
{
HttpResponse response = client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200)
{
FileOutputStream fos = new FileOutputStream(path);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
byte [] buffer = new byte[1024];
int count = 0;
while((count = in.read(buffer)) != -1)
{
fos.write(buffer, 0, count);
}
fos.close();
in.close();
entity.consumeContent();
result = true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Boolean result)
{
String path = databasePath + databaseFile;
if(result)
{
init_database(path);
}
}
}
}