0

I have the method showTrajects where I loop all the id of trajects I have, until here good.

Edit: I've looking and the problem is always getting the last ID of AllPendingTrajects, but why it's not getting in the first or second? The problem is inside AllPendingTrajects() :

public void showTrajects() {
        for (indexTrajecte = 0; indexTrajecte < contador; indexTrajecte++) {
            AllPendingTrajects();
        }
    }

 public void AllPendingTrajects(){

    AsyncHttpClient client = new AsyncHttpClient();
    String URL = "http://url.net/api/link1/"+IDTrajecte[indexTrajecte];
    client.get(URL,new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {}

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            JSONObject trajectes = new JSONObject();

            String strResponseBody = new String(responseBody);

            try {
                trajectes = new JSONObject(strResponseBody);
            }catch (JSONException e){
                Toast.makeText(PendingTrajectRecyclerView.this, "Error", Toast.LENGTH_SHORT).show();
            }

            try {
                JSONArray realitzas = trajectes.getJSONArray("realitzas");

                dniTaxista = trajectes.getString("taxistes_dni");
                idTraject = Integer.parseInt(trajectes.getString("ID"));

                if(DadesTaxista.DNI.equals(dniTaxista)){
                    showPendingTraject();
                }
                else {
                    SeeAllTrajects();
                }
            }catch (JSONException e){
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
            Log.d("ERROR",""+statusCode);
        }

    });
}

When I enter in the method SeeAllTrajects() in the log I'm always getting a random ID and I don't know what can be, it's like no sense, and I tried creating a global variable but without success

  public void SeeAllTrajects(){
    AsyncHttpClient client = new AsyncHttpClient();
    String URL = "http://url.net/api/link2";
    client.get(URL,new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            JSONArray trajectesJSON = new JSONArray();

            String strResponseBody = new String(responseBody);

            try {
                trajectesJSON = new JSONArray(strResponseBody);
            }catch (JSONException e){
                Toast.makeText(PendingTrajectRecyclerView.this, "Error", Toast.LENGTH_SHORT).show();
            }
            try {
                Log.d("ID TRAJECT:","d"+idByTraject);

                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Log.d("ERROR",""+statusCode);
        }
    });
}
Luis
  • 39
  • 1
  • 4
  • You send send the request like in the order `1 - 2 - 3` but you won't get response in the same order since the requests are Asynchronous. – K Neeraj Lal May 29 '18 at 10:16
  • So what can be the solution? – Luis May 29 '18 at 13:02
  • See [here](https://stackoverflow.com/questions/23485361/multiple-asynchttpclient-get-requests-to-populate-one-activity) and [here](https://github.com/AsyncHttpClient/async-http-client/issues/98). – K Neeraj Lal May 30 '18 at 06:26

0 Answers0