有第二个okHTTP请求等待第一个完成Android的操作

问题描述 投票:0回答:1

我尝试调用和IMDb API两次,第一次调用并获取该电影/节目的ID,第二次使用该ID获取有关该电影/节目的所有信息,我也需要应用程序另一部分的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 api android-studio okhttp
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.