有第二个 okHTTP 请求等待第一个完成 Android [英] Have second okHTTP request wait for the first to finish Android

查看:21
本文介绍了有第二个 okHTTP 请求等待第一个完成 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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.

这篇关于有第二个 okHTTP 请求等待第一个完成 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆