Search code examples
androidpostgetretrofitretrofit2

Retrofit 2 GET/POST doesn't work for large data in Android


I have seen other threads for this issue but unable to get any proper answer.

@POST("task/GetAllTasks")
Call<MyTask> getMyTasks(@Header("Authorization") String token, @Query("EmployeeId") String emp);

This is how I am calling, at first I thought it is due to GET request data limitation because GET imposes data limits and then I changed request from GET to POST but issue still persists.

ApiUtils.getTaskService().getMyTasks(apiToken, employeeId).enqueue(new Callback<MyTask>() {
@Override
        public void onResponse(Call<MyTask> call, Response<MyTask> response) {
   // ... Successful code goes here
 }

@Override
        public void onFailure(Call<MyTask> call, Throwable t) {
        //.. This block of code executing now :(
  }
 }

Always onFailure is being called. I have tested this same request on Postman and it is returning data. Content-Length is content-length →45720

It does work on small amount of data as I have tested it on Dev database which has smaller amount of data but on Live environment it is continuously causing problem.

Please suggest a solution or should I leave Retrofit and move to native Android library for this?

EDIT: Can we increase request timeout in Retrofit, if yes then how?


Solution

  • Try to increase your timeout:

    OkHttpClient client = new OkHttpClient().newBuilder()
                      .readTimeout(10, TimeUnit.SECONDS)
                      .connectTimeout(10, TimeUnit.SECONDS).build();
    

    And set it to your retrofit:

    new Retrofit.Builder().baseUrl("xxx").client(client).build().create(xxx.class);