我想使用async-http-client將數據發佈到服務器並保持設備上的SQLite與此數據同步。使用異步HTTP客戶端 Android async-http-client:爲回調添加參數?
- 惰性新數據的SQLite
- 後新的數據到服務器:所以基本上我想整個事情附加數據
我的問題是:我如何在AsyncHttpResponseHandler中獲得正確的行ID?
讓我們創建一個簡單的例子(沒有數據庫連接,以保持簡單但同樣的問題)。
private void addPerson(name){
//using a global client created like this in activity onCreate():
//AsyncHttpClient client = new AsyncHttpClient();
//here is a database insert creating a new row, returning the inserted id
int rowId = <some id returned by the insert>;
RequestParams param = new RequestParams();
param.put("name", name);
client.post("http://www.my.service.url", param, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
//The response i get contains an additional value, lets say userkey. I want to update the right database row with that information.
//How to get the right rowId here?
}
});
}
那麼實現這一目標的最佳方法是什麼?重寫AsyncHttpResponseHandler以某種方式將一個參數添加到回調函數?
我也實現了這一點作爲一種方式來做到這一點,但我不知道這是否是好的做法:是嗎?而且:如果我沒有訪問服務器,那麼這可能是一個問題。 – homtg
@homtg如果你沒有aceess,你可以通過改變參數來進行檢查,比如private void addPerson(rowId,name) – ceram1