1

Im using okhttp 2.5.0, im taking a pic, then converting it into base 64 format and then sending it to server as a post variable, then decoding it back to jpg format and saving to into the server.... when i use small images like 1 mega pixel (0.5mb) it works perfectly... but when I increase the resolution this Exception occurs...

09-10 23:29:47.673  25742-27156/in.definexit.chemapp W/System.err﹕ java.net.SocketTimeoutException: timeout
09-10 23:29:47.676  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.Okio$3.newTimeoutException(Okio.java:207)
09-10 23:29:47.676  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.AsyncTimeout.exit(AsyncTimeout.java:261)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.AsyncTimeout$1.write(AsyncTimeout.java:158)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.RealBufferedSink.write(RealBufferedSink.java:46)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:302)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
09-10 23:29:47.677  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.RealBufferedSink.write(RealBufferedSink.java:96)
09-10 23:29:47.679  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.RequestBody$2.writeTo(RequestBody.java:96)
09-10 23:29:47.679  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeOrCountBytes(MultipartBuilder.java:277)
09-10 23:29:47.682  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeTo(MultipartBuilder.java:297)
09-10 23:29:47.682  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:887)
09-10 23:29:47.682  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)
09-10 23:29:47.682  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.Call.getResponse(Call.java:268)
09-10 23:29:47.684  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
09-10 23:29:47.685  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
09-10 23:29:47.686  25742-27156/in.definexit.chemapp W/System.err﹕ at com.squareup.okhttp.Call.execute(Call.java:79)
09-10 23:29:47.686  25742-27156/in.definexit.chemapp W/System.err﹕ at in.definexit.chemapp.BGServerScripts.doInBackground(BGServerScripts.java:112)
09-10 23:29:47.686  25742-27156/in.definexit.chemapp W/System.err﹕ at in.definexit.chemapp.BGServerScripts.doInBackground(BGServerScripts.java:27)
09-10 23:29:47.686  25742-27156/in.definexit.chemapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
09-10 23:29:47.686  25742-27156/in.definexit.chemapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 23:29:47.687  25742-27156/in.definexit.chemapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-10 23:29:47.687  25742-27156/in.definexit.chemapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-10 23:29:47.687  25742-27156/in.definexit.chemapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-10 23:29:47.687  25742-27156/in.definexit.chemapp W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
09-10 23:29:47.687  25742-27156/in.definexit.chemapp W/System.err﹕ Caused by: java.net.SocketException: Socket closed
09-10 23:29:47.690  25742-27156/in.definexit.chemapp W/System.err﹕ at libcore.io.Posix.sendtoBytes(Native Method)
09-10 23:29:47.690  25742-27156/in.definexit.chemapp W/System.err﹕ at libcore.io.Posix.sendto(Posix.java:206)
09-10 23:29:47.690  25742-27156/in.definexit.chemapp W/System.err﹕ at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:513)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.Okio$1.write(Okio.java:80)
09-10 23:29:47.691  25742-27156/in.definexit.chemapp W/System.err﹕ at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
09-10 23:29:47.692  25742-27156/in.definexit.chemapp W/System.err﹕ ... 22 more

i tried increasing post and upload size in php.ini, increasing the timout of asynctask and okhttp client, nothing worked

3 Answers3

2

You are using Base64 to send images. Base64 is basically an ASCII representatioin of binary data which allows us to embed data in text streams such as JSON but it should also be noted that size of data is increased in using it. You are getting the SocketTimeoutException because either the data is large and upload call is not completed before timeout or the network is slow and the image doesn't get uploaded within the specified time.

Using multipart/form-data is the standard way of transferring binary in HTTP requests. It is always better to use this approach unless there are some limitations in doing so.

android_dev
  • 128
  • 7
  • i am using multipart/form-data from okhttp api... can u please give me some idea to fix this??? – Adam Saudagar Sep 11 '15 at 07:11
  • If you are using multipart/form-data then what's the necessity of Base64. It will increase the size of the data being sent. Are you able to upload an image without using Base64 encoding. – android_dev Sep 16 '15 at 17:23
1

I solved that problem increasing writeTimeout. In okhttp3 try:

OkHttpClient.Builder builder = new OkHttpClient().
builder.connectTimeout(5, TimeUnit.MINUTES);
writeTimeout(5, TimeUnit.MINUTES);
readTimeout(5, TimeUnit.MINUTES);
okHttpClient = builder.build();
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
0

It is throwing SocketTimeoutException simply extend the timeout.

or add more code to resize the image before uploading, you want to have a limit anyway.

meda
  • 45,103
  • 14
  • 92
  • 122
  • @meda i dont want to reduce the size of the image... as it will be pic of written paper quality of the image is essential... I tried increasing the timeout by using `client.setConnectTimeout(15, TimeUnit.SECONDS);` but from [link](http://stackoverflow.com/questions/25953819/how-to-set-connection-timeout-with-okhttp) but it didnt help – Adam Saudagar Sep 11 '15 at 07:06
  • @meda [Scripts](https://drive.google.com/folderview?id=0B1jzoj6y0Qb7TGdvSHlYZm5tQkE&usp=sharing) BGServerScript handles async task, CompleteTheOrderFragement runs the bgServerScript uploading the image, and serverImageUpload is the server script – Adam Saudagar Sep 11 '15 at 08:57
  • The Code - https://drive.google.com/folderview?id=0B1jzoj6y0Qb7TGdvSHlYZm5tQkE&usp=sharing – Adam Saudagar Sep 11 '15 at 16:17
  • I will put your question on bounty as I am facing the same problem. – Muhammad Irfan Sep 30 '16 at 11:39