1

I have to send some things from the database to the server. So, I call a db method, and get a cursor, which has many positions. I need to send a httprequest for avery position of the cursor, but only send the second petition when the first has been received in the server, and the proper answer sent. I have this code:

final Cursor cursor = db.getData();
Thread thread=new Thread(new Runnable() {
      @Override
      public void run() {
           if(cursorContadores.moveToFirst()){
                  do{
                  Call<String> peticion = interfaz.saveToServer(cursor.getString(1), cursor.getString(2));
                  peticion.enqueue(new Callback<String>() {
                       @Override
                       public void onResponse(Response<String> response, Retrofit retrofit) {

                              if(response.message().equals("ok")){

                              }else{

                              }
                       }

                       @Override
                       public void onFailure(Throwable t) {

                                            }
                                        });

                  }while (cursorContadores.moveToNext());
           cursorContadores.close();
           db.close();
                  }
                            }
        });
thread.start();

This way, I think it will not wait until every iteration in the do while block ends, to start the next iteration.

How could I achieve this? Something I could read to learn how to?

Thank you.

Fustigador
  • 6,096
  • 11
  • 56
  • 109
  • 2
    Why not send the second request on Success of your first request. And also check the expected response of first request before sending second. – Ritt Oct 28 '15 at 11:11
  • register a brodcast receiver . call it when a http response is got from server (for success -status code is 200), send even position of ur cursor, then catch it in onreceive and then u can start the next http request from on receive of particular cursor position. – Mahalakshmi Oct 28 '15 at 11:26

2 Answers2

0

In your Http response, you can check for response code. For example if the response is 200 for successful receive, you can do something like:

HTTPResponse response;

if(response.getStatusLine().getStatusCode()==200){

 //SEND NEXT

}

This way you can send the next request once the previous one is received. I hope I understood the question correctly. Thank you.

Prakhar
  • 748
  • 6
  • 23
  • Yeah, you understood it correctly. Thanks for your answer. – Fustigador Oct 28 '15 at 11:17
  • Well, I cannot check it because the web service is not ready yet, and I am trying to get ahead in my job...but the answer makes sense to me. I will wait and if no one gives a better solution, will upvote your answer. – Fustigador Oct 28 '15 at 11:21
  • Think I got it...we will see when web service is ready. By now, my code looks ok. – Fustigador Oct 28 '15 at 11:57
0

This can most definitely be done AsyncTask... Handle the network request in doInBackground() and once doInBackground() is finished, onPostExecute() is triggered on the response and that's where you can execute any code that send the second petition.

If you need something a bit more generic and re-usable, you would probably want to implement a callback.. I'll refer to the next step as the client and the AsyncTask as the server.

Create a new interface and create some method stubs.

public interface MyEventListener {
    public void onEventCompleted();
    public void onEventFailed();
} 

Have your client pass instance of MyEventListener to the server. A typical way of doing this is to have your client implement the interface (MyEventListener) and pass itself to the server.

public class MyActivity implement MyEventListener {

public void startEvent() {
    new MyAsyncTask(this).execute();
}           

@Override
public void onEventCompleted() {
    // TODO
}

@Override
public void onEventFailed() {
    // TODO
  }
}

On the onPostExecute of the server, check if the callback is null and call the appropriate method.

    public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    private MyEventListener callback;

    public MyAsyncTask(MyEventListener cb) {
        callback = cb;
    }

    [...]

    @Override
    protected void onPostExecute(Void aVoid) {
        if(callback != null) {
            callback.onEventCompleted();
        }
    }
}

Check similar reference

learn more reference

Community
  • 1
  • 1
Saurav Wahid
  • 371
  • 2
  • 8