Retrofit socket timeout exception while uploading larger files using multipart
Asked Answered
M

2

4

I am facing the issue of socket timeout exception while using retrofit 2.0.2 library and okhttp 2.3.0. I am trying to upload the image file which is between 500kb to 1.5mb it is uploading successfully.but when i tried to upload video file which is greater than 5mb i am getting this exception.

I used httpclient for connection settings as below.

public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(0, TimeUnit.SECONDS)
            .writeTimeout(0, TimeUnit.SECONDS)
            .readTimeout(0, TimeUnit.SECONDS)
            .build();

please suggest me to upload larger files without this issue.Thanks in advance

Moussaka answered 13/11, 2017 at 8:13 Comment(1)
A post method can have maximum body of upto 4 mB irrespective of it being multipart, u gotta us a custom FTP provider, or u have to Implement a custom mechanism for such uploads, Split file in Chunks, upload them to server, and then merge them on server and save themPostbellum
R
1

you can provide the time in seconds as follows
public class ApiClient {

public static final String BASE_URL = "your_url";
public static Retrofit retrofit = null;

public static Retrofit getApiClient() {
    if (retrofit == null) {
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .build();
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();


    }

        return retrofit;
}

}

i've given 60 seconds

Reft answered 13/11, 2017 at 9:20 Comment(3)
even if i give 60 seconds i am getting the same error..Is there any way to retry after connection is timed out?Moussaka
try to give 2 minutes like 120 seconds, and if i get any solution, i'll tell you.Reft
Did you get any solution? @HarikrishnaWinwaloe
U
0

There can be two issues with this type of error.

  1. Check Read & Write Timeout
val client = OkHttpClient.Builder()
    .connectTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(30, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build()
  1. Check the Mime type you are sending. The backend developer may have filtered the accepted Mime type at their end. instead of MediaType.parse("multipart/form-data"). write the valid mime type for files like image/jpg or video/mp4
MediaType.parse("image/png")
Umbel answered 29/9, 2021 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.