Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取随机ID_Java_Android_Asynchttpclient - Fatal编程技术网

Java 获取随机ID

Java 获取随机ID,java,android,asynchttpclient,Java,Android,Asynchttpclient,我有方法showtracts其中我循环所有id的轨迹,直到这里好 编辑:我一直在寻找,问题总是获取所有PendingTraects的最后一个ID,但为什么不获取第一个或第二个ID? 问题出在AllPendingTrajects()内部。: 您可以按照1-2-3的顺序发送请求,但由于请求是异步的,因此不会以相同的顺序得到响应。那么解决方案是什么呢?请参阅和。 public void showTrajects() { for (indexTrajecte = 0; indexTraj

我有方法
showtracts
其中我循环所有
id
轨迹,直到这里好

编辑:我一直在寻找,问题总是获取所有PendingTraects的最后一个ID,但为什么不获取第一个或第二个ID? 问题出在
AllPendingTrajects()内部。


您可以按照
1-2-3
的顺序发送请求,但由于请求是异步的,因此不会以相同的顺序得到响应。那么解决方案是什么呢?请参阅和。
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);
        }

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