1
 var client = OkHttpClient()
            val builder = OkHttpClient.Builder()
            val gson = GsonBuilder()
                .setLenient()
                .create()

                builder.addInterceptor(AddCookiesInterceptor(mcontext))
                builder.addInterceptor(ReceivedCookiesInterceptor(mcontext))
                builder.callTimeout(100,TimeUnit.SECONDS)

            client = builder.build()
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
 //                   .addConverterFactory(ScalarsConverterFactory.create())
                .build()

This is my retrofit client service builder. For normal api functions with json response callback it works fine. Is there any modification required for an large file upload?

With current scenario, uploading 20 mb data, takes more time in slow network connection which returns a socket timeout exception.

Uploading as multipart body

 var fileBody : ProgressRequestBody? = null
   fileBody = ProgressRequestBody(file,"*/*",this@CaseFileUploadFragment)
        var fileToUpload: MultipartBody.Part = 
   MultipartBody.Part.createFormData("image",file.name, fileBody)
        var filename : RequestBody = 
    RequestBody.create(MediaType.parse("text/plain"),file.getName())

and following is the function used

 @Multipart
    @POST("{urlMpString}")
    fun uploadFile(
        @Path ("urlMpString") urlEndString : String, @Part file: MultipartBody.Part, @Part("file") requestBody: RequestBody,
        @Part("apiInfo") `object1`: JsonObject,  @Part("parameters") `object2`: JsonObject
    ): Call<JsonObject>

Everything works fine for small data files. Any suggestions and help will be appreciated. Thanks

SARATH V
  • 500
  • 1
  • 7
  • 33
  • Maybe you need a large timeout from your server, check with the web service and see whats the timeout there and check if the connection is continuously opened. I have used only small files around 50 MB and it worked fine – Rahul Singh Chandrabhan May 19 '20 at 17:09
  • @RahulChandrabhan Can you help? https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-using-retrofit-2-in-android?noredirect=1#comment111031344_62783444 – Priyanka Singh Jul 08 '20 at 07:27

1 Answers1

1

Try below code for okHttpClient

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
    .connectTimeout(5, TimeUnit.MINUTES)
    .readTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(15, TimeUnit.SECONDS)
    .build();
Dhruv garg
  • 689
  • 7
  • 22