让第二个 okHTTP 请求等待第一个完成 Android

Have second okHTTP request wait for the first to finish Android

我正在尝试调用 IMDb API 两次,第一次调用并获取 movie/show 的 ID,第二次使用该 ID 获取有关的所有信息movie/show,我还需要该应用程序的另一部分的 ID,这就是我这样做的原因。问题是第二个电话没有等待第一个电话完成。我认为这就是为什么在我尝试使用变量时变量没有更新的原因。这是我的 onCreate 方法,所有这一切都发生了,出于显而易见的原因,我取出了一些 API 键:

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imdb_activity);

        mTextViewResult = findViewById(R.id.text_view_result);

        OkHttpClient client1 = new OkHttpClient();
        //change the url to be generic and usable for user input
        String urlforID = "https://movie-database-imdb-alternative.p.rapidapi.com/?page=1&r=json&s=Avengers";
        final Request request = new Request.Builder()
                .url(urlforID)
                .get()
                .addHeader("x-rapidapi-host", "movie-database-imdb-alternative.p.rapidapi.com")
                .addHeader("x-rapidapi-key", "KEYGOESHERE")
                .build();
        client1.newCall(request).enqueue(new Callback()
        {
            @Override
            public void onFailure(Call call, IOException e)
            {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException
            {
                if(response.isSuccessful())
                {
                    String myResponse = response.body().string();
                    try
                    {
                        myObj = new JSONObject(myResponse);
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }

                    try
                    {
                        myArray = myObj.getJSONArray("Search");

//                responseID = new String[myArray.length()];//might have to subtract 1

                        for(int i = 0; i < myArray.length(); i++)
                        {
                            JSONObject obj1 = myArray.getJSONObject(i);
                            responseID[i] = obj1.getString("imdbID");
//                            Log.d("id","the id that was just put in was: " + responseID[i]);
                        }
                    }
                    catch(JSONException e)
                    {
                        e.printStackTrace();
                    }

                    imdb_activity.this.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            //new ReadJsonForID().execute();
                            Log.d("id", "The id is: " + responseID[0]);
                            mTextViewResult.setText(responseID[0]);
                        }
                    });
                }
            }
        });
//this is where call 2 would happen but it is not saving the variable how it should
            Log.d("id", "The id is after finish: " + mTextViewResult.getText());

您可以在包 java.util.concurrent 中使用 CountDownLatch-class。在第一次调用中,countDownLatch 被实例化,在第二次调用中,您等待 CountDownLatch。这可能需要将第二个任务放在 AsyncTask.