如何使用 Retrofit 拨打多个电话? [英] How to make multiple calls with Retrofit?

查看:63
本文介绍了如何使用 Retrofit 拨打多个电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Retrofit 多次调用 API REST 并在 ListView 中显示响应,但我不知道如何执行此操作,此代码不起作用.

I need to make multiple calls to API REST with Retrofit and show the response in a ListView, but I don't know how to do this and this code doesn't work.

型号

@GET("apks/{sha256}")
    Call<DatoAPI> getTask2(@Path("sha256") String hash, @Query("Authorization") String key);

实施

for (String s: hash) {                                          
    Call<DatoAPI> call = services.getTask2(s, API.API_KEY);
    call.enqueue(new Callback<DatoAPI>() {
        @Override
        public void onResponse(Call<DatoAPI> call, Response<DatoAPI> response) {
            if (response.isSuccessful()) {
                datoAPI = response.body();
                items.add(datoAPI.getApp());
            }
        }

        @Override
        public void onFailure(Call<DatoAPI> call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
}

我也试过 call.execute() 和同样的问题我想在 ListView 中显示此响应,但它不起作用.

Also I tried with call.execute() and same problem I want to show this response in a ListView but it doesn't work.

推荐答案

首先需要了解 Retrofit 的 Call#enqueue()Call#execute() 方法.

First of all you need to understand the differences between Retrofit's Call#enqueue() and Call#execute() methods.

  1. enqueue() 方法是异步,这意味着您可以在完成之前转移到另一个任务

  1. enqueue() method is Asynchronous which means you can move on to another task before it finishes

execute() 方法是 Synchronous 这意味着,您等待它完成后再继续执行另一个任务.

execute() method is Synchronous which means, you wait for it to finish before moving on to another task.

在您的情况下,您使用 for 循环在一个范围内执行多个请求.

And in your case, you're using for loop to execute multiple requests in a single stretch.

现在,如果使用for循环来执行网络操作,网络操作不会停止for循环进入下一次迭代.不要期望 API 会总是以足够快的方式响应,然后再进入下一次迭代循环.这是个坏主意.

Now, if you use for loops to execute network operation, the network operation will not stop for loops from going to the next iteration. Do not expect that the API will always respond in a fast enough way before going to for loops next iteration. That's a bad idea.

如果你使用 Retrofit 的 execute() 方法,它不会允许你继续下一行(或迭代)作为它的同步行为,而且它会抛出 NetworkOnMainThreadExceptionIOException.因此,您需要将请求包装在 AsyncTask 中并处理 IOException.

If you use Retrofit's execute() method, it will not allow you to continue to next line (or iteration) as its Synchronous behavior, plus it throws NetworkOnMainThreadException and IOException. Hence, you need to wrap the request in an AsyncTask and handle IOException.

我建议您使用 RxAndroidRxJava 而不是使用 for 循环.有很多关于这个主题的教程.

I'd recommend you to use RxAndroid with RxJava instead of using for loops. There are plenty of tutorials out there on this topic.

请参考以下 StackOverflow 问题来解决您的问题.

Refer to the following StackOverflow questions to solve your problem.

  1. 如何在 Retrofit 2.0 - Android 中发出多个请求并等待数据来自所有请求?立>
  2. 异步与同步执行,真正意味着什么?

根据您的要求调整代码.

Adjust the code as per your requirements.

祝你好运!

这篇关于如何使用 Retrofit 拨打多个电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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