在收到第一个请求的响应后调用第二个Retrofit请求

时间:2017-02-04 04:28:41

标签: android request retrofit retrofit2

第一次请求: ReceiveNewDataFromServer()

第二次请求: SendNewDataToServer()

目前,这两个请求都与成功的响应平行发送,但有时会与数据发生冲突。

对于一个接一个的请求有什么解决方案吗?

P.S。我想把第二个请求放在"成功"首先回调,但可能需要更多时间。

3 个答案:

答案 0 :(得分:1)

嗨,你可以在android中使用RX java这些目的基本的例子我会告诉你。您可以使用 ZIP 运算符。

fetchFromGoogle = fetchFromGoogle.subscribeOn(Schedulers.newThread());

fetchFromYahoo = fetchFromYahoo.subscribeOn(Schedulers.newThread());

要处理两个请求需要并行运行的第一个场景,您可以使用zip运算符并订阅它返回的Observable。

// Fetch from both simultaneously
  Observable<String> zipped  = Observable.zip(fetchFromGoogle, fetchFromYahoo, new Func2<String, String,  String>() {
  @Override 
  public String call(String google, String yahoo) {
      
// Do something with the results of both threads

            return google + "\n" + yahoo;

  }
     });

以下是example我们如何通过改装中的RX java来调用apis。这是具有最新技术的Android应用程序架构。

答案 1 :(得分:0)

尝试使用EventBus。使用evenbus http://www.devexchanges.info/2016/01/publishing-and-receiving-events-with.html

检查网络连接的示例

答案 2 :(得分:0)

这是对不了解RxJava的人的答案。如果您对RxJava不满意,则可以使用改造中的嵌套调用,但是如果要管理的代码更多,则建议使用RxJava。

apiInterface = ApiClient.getClient().create(ApiInterface.class);

        Call<MovieResopnse> call = apiInterface.getTopRatedMovie(API_KEY);
        final Call<List<Language>> callLanguage = apiInterface.getLanguages(API_KEY);

        call.enqueue(new Callback<MovieResopnse>() {
            @Override
            public void onResponse(Call<MovieResopnse> call, Response<MovieResopnse> response) {
                movies = response.body().getResults();
                Log.d(TAG, "onResponse: Number of movies received: " + movies.size());

                Picasso.get()
                        .load(IMAGE_URL + movies.get(item).getPosterPath())
                        .into(image);

                title.setText(movies.get(item).getOriginalTitle());
                subtitle.setText(movies.get(item).getTitle());
                description.setText(movies.get(item).getOverview());
                rating.setText("" + movies.get(item).getVoteAverage());
                release_date.setText("Release: " + movies.get(item).getReleaseDate());

                // Request another api after getting language code from the first one.
                callLanguage.enqueue(new Callback<List<Language>>() {
                    @Override
                    public void onResponse(Call<List<Language>> call, Response<List<Language>> response) {
//                languageList = new ArrayList<>();
                        Log.d(TAG, " Request url : " + call.request().url().toString());
                        languageList = response.body();

                        for (Language language1 : languageList) {
                            if (language1.getIso6391().contains(movies.get(item).getOriginalLanguage()))
                                language.setText(language1.getEnglishName());
                        }
                        Log.d(TAG, "onResponse: " + languageList.size() + " Languages");
//                Log.d(TAG, "onResponse: "+languageList.get(0).getEnglishName());
                    }

                    @Override
                    public void onFailure(Call<List<Language>> call, Throwable t) {
                        // Log error here since request failed
                        Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "onFailure: " + t.getMessage());
                        Log.d(TAG, "onFailure: request url : " + call.request().url().toString());
                    }
                });


            }

            @Override
            public void onFailure(Call<MovieResopnse> call, Throwable t) {
                // Log error here since request failed
                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onFailure: " + t.getMessage());
                Log.d(TAG, "onFailure: request url : " + call.request().url().toString());

            }
        });