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

时间:2019-02-14 11:21:57

标签: 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");

        }
    });

我有这段代码,它使用request方法将async传送到服务器。我想要多次发出相同的请求。例如,我想做get request 100次。如果可能的话,我不想使用Observable或其他外部库。有谁可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

创建一个广播接收器,然后在其中添加您的改装请求。现在,使用警报管理器对其进行调用。

public class YourBroadCastReciever extends BroadcastReceiver{



    @Override
    public void onReceive(Context context, Intent intent) {

            yourRetrofitCode();

    }

    private void yourRetrofitCode(){
        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");

        }
    });

    }
}

现在使用此代码致电:

 Intent sendDeviceInfoIntent = new Intent(this, YourBroadCastReciever.class);
       PendingIntent yourintent= PendingIntent.getBroadcast(this, 0, sendDeviceInfoIntent, 0);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+1000, 60000, yourintent);

这样每5分钟就会呼叫您的广播接收者,并且也会在后台运行

答案 1 :(得分:1)

您可以多次拨打电话,只需看下面的代码即可。在对从服务器获取的数据执行操作之后,在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");

            }
        });
    }
}