简体   繁体   中英

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?

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) ;

Use @Part to send big string data it can easily send your data to server side

For ex. You need to send some string that name is "abc" then in the retrofit code

@Part("abc") //your string variable

Its works for me and I save that data to mediumtext in mysql and the code is $_REQUEST['abc'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM