0

我想使用async-http-client将数据发布到服务器并保持设备上的 SQLite 与这些数据同步。所以基本上我希望整个事情:

  1. 将新数据惰性化到 SQLite
  2. 使用 async-http-client 将新数据发布到服务器
  3. 使用附加数据更新 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 以某种方式向回调函数添加参数?

4

2 回答 2

0

使你的变量最终,比如

final int rowId = <some id returned by the insert>;
于 2013-12-15T09:05:37.147 回答
0

很简单,你可以发送rowId。

param.put("_id", rowId);

并且,服务器将其发送回客户端。

这是一个好方法,因为它只允许您创建一个 AsyncHttpResponseHandler 实例。(最好尽量少实例化)

于 2013-12-15T09:05:50.437 回答