将此用作您的 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;
}
}
此类帮助您更新 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();
}
}
您必须根据从 DB 返回的数据来定制上述代码。希望能帮助到你:)