我想使用async-http-client将数据发布到服务器并保持设备上的 SQLite 与这些数据同步。所以基本上我希望整个事情:
- 将新数据惰性化到 SQLite
- 使用 async-http-client 将新数据发布到服务器
- 使用附加数据更新 onSuccess/onFailure 回调中的 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 以某种方式向回调函数添加参数?