Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to use async-http-client to post data to a server and keep a SQLite on device in sync with this data. So basically i want the whole thing to:

  1. Inert new data to the SQLite
  2. Post the new data to the server using async-http-client
  3. Update the SQLite row in the onSuccess/onFailure Callbacks with additional data

My Question is: How can i get the right row id in the AsyncHttpResponseHandler?

Lets create an easy example (no database connection to keep it simple but same problem).

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?
    }
  });

}    

So what is the best way to achieve this? Overriding AsyncHttpResponseHandler to somehow add a parameter to the callback functions?

share|improve this question

2 Answers 2

Make your variable final, like

final int rowId = <some id returned by the insert>;
share|improve this answer

It's quite simple, you can send rowId.

param.put("_id", rowId);

and, server send it back to the client.

It's good way, because it allows you to make only one AsyncHttpResponseHandler instance. (It's good to instantiate as less as possible)

share|improve this answer
    
I also implemented this as one way to do this but i was not sure if this is good practice: is it? And: What if i do not have access to the server then this might be a problem. –  homtg Dec 15 '13 at 9:08
    
@homtg If you don't have aceess, you can check by changing parameters like private void addPerson(rowId, name) –  ceram1 Dec 15 '13 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.