Have second okHTTP request wait for the first to finish Android

蹲街弑〆低调 提交于 2021-02-11 14:47:55

问题


I am trying to call and IMDb API 2 times, the first time it calls and gets the ID for that movie/show and the second time it uses that ID to get all the information about that movie/show, I also need that ID for another part of the app so that is why I am doing it this way. The problem is that the second call isn't waiting for the first call to be done. I think this is why the variables aren't updated when I am trying to use them. This is my onCreate method where all this is happening, I took out some of the API key for obvious reasons:

@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());

回答1:


You could use CountDownLatch-class in package java.util.concurrent. In the first call, the countDownLatch is instantiated and in the second call, you await the CountDownLatch. This maybe require to put the second task in an AsyncTask.



来源:https://stackoverflow.com/questions/60792549/have-second-okhttp-request-wait-for-the-first-to-finish-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!