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,339
  • 12
  • 59
  • 115
  • 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

1 Answers1

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Prakhar
  • 710
  • 6
  • 24