android - Retrofit 2 如何多次发出相同的请求

标签 android http retrofit2

public void makeGetRequest() {


    Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://desolate-beach-17272.herokuapp.com");
    Retrofit retrofit = builder.build();


    RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);


    Call<ResponseBody> call = retrofitInterface.downloadFileByUrl("downloadFile/beach.jpg");


    call.enqueue(new Callback<ResponseBody>() {
        @SuppressLint("StaticFieldLeak")

        // returns the response if everything is okay
        @Override
        public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {

            try {
                Log.d("Success" , " " + response.body().bytes().length);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.d("FAIL", "oops");

        }
    });

我有这段代码,它使用 async 方法向我的服务器发出 get request 。我想要的是多次提出相同的请求。例如,我想发出 get 请求 100 次。如果可能的话,我不想使用可观察对象或其他外部库。有谁可以帮我吗?

最佳答案

您可以多次调用电话,只需查看下面的代码,我在其中使用了用于此目的的方法。在 onResponse 方法中,对从服务器获取的数据执行操作后,您可以通过使用变量 sizeOfCall 并递减它直到等于零来再次调用同一 API。下面是它的完整代码。

public class RequestActivity extends AppCompatActivity {

    int sizeOfCall = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_request);

      callAPI();
    }

 private void callAPI(){
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://desolate-beach-17272.herokuapp.com");
    Retrofit retrofit = builder.build();
    RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
    Call<ResponseBody> call = retrofitInterface.downloadFileByUrl("downloadFile/beach.jpg");

    // Call API
    makeGetRequest(call);
}

 private void makeGetRequest(Call<ResponseBody> call) {

call.enqueue(new Callback<ResponseBody>() {

   @SuppressLint("StaticFieldLeak")

    // returns the response if everything is okay
    @Override
    public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {

        try {
            Log.d("Success", " " + response.body().bytes().length);
            // Perform your operations here and call API againg after that
            sizeOfCall--;
            if (sizeOfCall > 0) {
                callAPI();
                } else {
                 // No more calls needed
                 }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("FAIL", "oops");

            }
        });
    }
}

关于android - Retrofit 2 如何多次发出相同的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54689233/

相关文章:

android - 手机间隙 : GCM Push Notification registration call returns OK but device is not even connected to the internet

android - 如何在 android ICS 上使用 'exec app_process' 启动 jar

json - Swift - 在 Post 请求上向 httpBody 添加参数

java - 使用 Single 获取改造 onError 响应

php - Retrofit 2 Multipart POST 请求向 PHP 发送额外的报价

php - 我想知道为什么不向MySQL发送数据?

android - 抽屉 android,删除箭头

web-services - 由于可以嗅探请求的参数,API key 是否在 http 上毫无值(value)?

python - Curl 可以工作,但 python requests 不行

android - 从 recyclerview 获取详细信息并使用 cardview 和 retrofit 2 网络库在另一个 fragment 中显示结果