Sye*_*Ali 3 java sql-server android synchronization asynchronous
我正在开发一个具有的Android应用程序
同步将在1天内自动运行
现在的问题是如何在Android中同步这些数据库?因为我刚刚进入移动世界,我也是internee,他们说,如果我将完成这项工作,那么我将是永久性的.
发现了几件事,但我在初学者一级.我找不到任何样品.我在这里发现了类似的问题,但没有找到任何代码,任何只在2路上同步数据的样本.
如果有人会给我(开源项目)或一些带有一些解释的示例代码,我将不胜感激.
或类似的东西,将告诉我一个方法来做到这一点.
如何在Android手机上将SQLite数据库与服务器上的MySQL数据库同步?
我找到了这些文章,但没有帮助我.
使用此作为您的Android服务
public class Connector {
HttpClient httpClient = null;
HttpPost httpPost = null;
HttpResponse httpResponse = null;
String serverResponse = null;
public String callService(String url){
try{
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
httpResponse = httpClient.execute(httpPost);
//Log.e("URL Response", serverResponse);
serverResponse = EntityUtils.toString(httpResponse.getEntity());
}
catch(Exception ex){
Log.e("Exception in connectivity", ex.toString());
}
//Log.e("URL Response", serverResponse);
return serverResponse;
}
}
Run Code Online (Sandbox Code Playgroud)
这个类可以帮助你更新android db
public class DbSync extends SQLiteOpenHelper {
private static String DB_PATH = "";
private static final String DATABASE_NAME = "Your_Db_Name";
private SQLiteDatabase sqliteDb = null;
private String path = null;
public DbSync(Context context) {
super(context, DATABASE_NAME, null, 1);
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
path = DB_PATH + DATABASE_NAME;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void syncUsers(){
try {
String response = new TaskAsync().execute("URL_Where_Service_Running").get();
Log.d("response", response);
String tableName = "Table_Name";
sqliteDb = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
sqliteDb.delete(tableName, null, null);
//here you have to parse your data from DB and insert using
ContentValues values = new ContentValues();
values.put("col_name1", parsedColumnValue);
values.put("col_name2", parsedColumnValue);
long status = sqliteDb.insert(tableName, " ", values);
Log.d("database", Long.toString(status));
sqliteDb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
您必须根据从DB返回的数据定制上述代码.希望能帮助到你:)
归档时间: |
|
查看次数: |
11215 次 |
最近记录: |