【问题标题】:Have second okHTTP request wait for the first to finish Android让第二个 okHTTP 请求等待第一个完成 Android
【发布时间】:2020-07-02 16:13:28
【问题描述】:

我尝试调用 IMDb API 2 次,第一次调用并获取该电影/节目的 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());

【问题讨论】:

  • 如何执行第二次调用?代码-sn-p 只显示一个调用。
  • @J.Gerbershagen 我实际上做了同样的事情,比如进行调用,然后在第一个调用的主体之外执行它,这样它就在 onCreate() 内部。我应该从第一个内部拨打第二个电话吗?

标签: java api android-studio okhttp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 2020-02-10
    • 2020-11-01
    • 2021-01-04
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多