fuel icon indicating copy to clipboard operation
fuel copied to clipboard

SocketTimeoutException when trying to upload a file

Open andrewmarmion opened this issue 7 years ago • 10 comments

I am using fuel and fuel-android 1.12.0 I keep getting the following error when trying to upload a file.

This is the response:

<-- -1 (https://myurlhere.com)
Response : 
Length : 0
Body : (empty)
Headers : (0)

This is the result [Failure: java.net.SocketTimeoutException: Read timed out]

The file I am trying to upload is a bitmap that I am scaling then compressing:

val file = File.createTempFile( "file", ".png")
val fOut = FileOutputStream(file)

val height = (bitmap.height * 128 / bitmap.width)
val newBitmap = Bitmap.createScaledBitmap(bitmap, 128, height, true)
newBitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut)
fOut.flush()
fOut.close()

My request looks like this

Fuel.upload(url)
.source { request, url -> file }
.name { "file" }
.responseString { request, response, result -> 
    handler(response.statusCode)
}

I have tried changing the default timeout and readtimeout but that hasn't had any affect.

This works in the emulator, however when I try it on a device I get the above response and result. I have tried different devices running different version of Android, from Android 21 to 26 all with the same outcome. It's not even hitting the server that the url points to.

I was wondering if anyone had any suggestions on what I can do to fix this?

andrewmarmion avatar Dec 13 '17 16:12 andrewmarmion

So there seems to be some kind of buffer issue. I have tried shrinking the file size down and it would appear that if the file size is over 1KB I get the SocketTimeoutException. Is there anyway that I can increase the buffer size or change my underlying code so that it works?

andrewmarmion avatar Dec 14 '17 12:12 andrewmarmion

Any update on this, I am getting same issue in Android. Gradle version: 1.12.1

jigarpatel17 avatar Apr 25 '18 07:04 jigarpatel17

@jigarpatel17 I was uploading an image and resolved it in the following way.

fun uploadAvatar(bitmap: Bitmap, handler: (Int) -> Unit) {
  val file = File.createTempFile("file", ".png")
  val fOut = FileOutputStream(file)

  val height: Int = (bitmap.height * 128 / bitmap.width).toInt()
  val newBitmap = Bitmap.createScaledBitmap(bitmap, 128, height, true )
  newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut)
  fOut.flush()
  fOut.close()
  System.gc()

  val request = Fuel.upload("URL")

  request.source { request, url -> file }
    .name { "file" }
    .responseJson { request, response, result ->
                        //handle response here
   }
}

However I never got it working to upload larger files.

andrewmarmion avatar Apr 25 '18 08:04 andrewmarmion

@andrewmarmion It seems like you are compressing bitmap and it may reduce the quality. Was looking for a better way to do so and found below one which might help you in your case.

Fuel.upload("PUT YOUR WEB URL HERE",
        Method.POST, requestParam)
        .dataParts { request, url ->
            listOf(
                    //DataPart takes a file, and you can specify the name and/or type
                    DataPart(localVideoFile, "video"),
                    DataPart(localThumbnailFile, "thumbnail_image"))
        }
        .timeoutRead(10 * 60 * 1000) // this mean 10 minutes
        .timeout(10 * 60 * 1000) // this mean 10 minutes
        .responseString { request, response, result ->
            Log.d(TAG, "Upload response code: ${response.statusCode}")
        }

jigarpatel17 avatar Apr 25 '18 09:04 jigarpatel17

@jigarpatel17 Yeah I am compressing it, however the image that I needed to upload was for an avatar that was quite small, so I wasn't worried about loss of quality.

I had tried using the method that you suggest but it never worked for me. For uploading larger files I wrote a custom implementation that didn't involve using Fuel.

Thanks for your response. I may try your suggestion again if and when I need to update the app, but it is currently working as I need it to so I will just leave it for the time being.

andrewmarmion avatar Apr 25 '18 14:04 andrewmarmion

The timeout() and timeoutRead() methods helped me with this matter.

carlos-mg89 avatar Mar 01 '19 15:03 carlos-mg89

I'm having the exactly same problem, specially if I do multiple uploads at same time, if you try to upload 100 * 1MB file, on the 30th file the problems happens and the lib crashes.

abraaocaldas avatar Aug 21 '19 19:08 abraaocaldas

If you're doing multiple uploads at a time, and don't manage how many are active, it will load all the streams into memory at the same time. Fuel doesn't pool requests at the moment, so you're responsible for that.

Memory issues, timeout issues, and such are usually all similar: too many concurrent uploads. The receiving server probably can not accept the sockets all at the same time.

SleeplessByte avatar Aug 21 '19 23:08 SleeplessByte

If you're doing multiple uploads at a time, and don't manage how many are active, it will load all the streams into memory at the same time. Fuel doesn't pool requests at the moment, so you're responsible for that.

Memory issues, timeout issues, and such are usually all similar: too many concurrent uploads. The receiving server probably can not accept the sockets all at the same time.

So what is the maximum file for upload? Because 10MB of data seems very little to me (10 upload instances of 1MB at any given time).

I don't see the issue using postman for example, even trying to upload bigger files at same time, I'm sure that the server side is not the problem.

abraaocaldas avatar Aug 22 '19 00:08 abraaocaldas

There is no maximum file upload size. Postman will limit the number of concurrent uploads automatically. Your browser limits it to ~6 (depending on the browser and version). Fuel does not limit it.

SleeplessByte avatar Aug 22 '19 05:08 SleeplessByte