Search code examples
androidretrofitokhttpsocket-timeout-exception

Modify File Upload Socket Timeout and Exception


In my android app when I want to upload a file with size of 2MB and more, it throws this Exception:

java.net.SocketTimeoutException: timeout at java.lang.Thread.run(Thread.java:841)
caused by: java.net.SocketException: Socket is closed at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.checkOpen(OpenSSLSocketImpl.java:237) at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$100(OpenSSLSocketImpl.java:63) at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:723) at
okio.Okio$1.write(Okio.java:80) at
okio.AsyncTimeout$1.write(AsyncTimeout.java:155)

There is no problem with uploading less than 2MB and every thing work fine.

How do I modify file upload socket timeout and exception?


Solution

  • ok, for one whom might get stuck on this like me I must say there is some reasons for this exception like weak connection,wifi poor signal,low read write and connect time out and etc...

    check all the situations and if did't got work you can use below lines:

    i ended up this by adding this line of code:

    OkHttpClient.setRetryOnConnectionFailure(true);
    

    and setting more time out period

    OkHttpClient.setConnectTimeout(10, TimeUnit.MINUTES);
    OkHttpClient.setReadTimeout(10, TimeUnit.MINUTES);
    OkHttpClient.setWriteTimeout(10, TimeUnit.MINUTES);
    

    I want to know if setting this 10 minutes may cause a problem? i will be so grateful if some one could tell me