java - HttpURLConnection's getOutputStream throwing weird IOException -
i'm using httpurlconnection upload large files (up 160mb) server. there weird ioexception coming randomly. here code.
i record video file(max. 5 min). upload starts intentservice. uses httpurlconnection. here how initiated httpurlconnection.
try { url url=new url(getstring(posturlresid)); httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setdoinput(true); httpurlconnection.setdooutput(true); httpurlconnection.setconnecttimeout(60000); //httpurlconnection.setusecaches(false); httpurlconnection.setfixedlengthstreamingmode(postlength); httpurlconnection.setrequestmethod("post"); //httpurlconnection.setrequestproperty("connection", "keep-alive"); //httpurlconnection.setrequestproperty("cache-control", "no-cache"); httpurlconnection.setrequestproperty("content-type", "multipart/form-data;boundary=*****"); dataoutputstream = new dataoutputstream(httpurlconnection.getoutputstream()); dataoutputstream.writebytes(header); ----------- ----file writting dataoutputstream --------- }
dataoutputstream.writebytes(footer); response_upload_video_being_processed_in_server(); if(httpurlconnection.getresponsecode() == 200 && httpurlconnection.getresponsemessage().equalsignorecase("ok")) { bufferedreader bufferreader = new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream())); stringbuffer serverres = new stringbuffer(); string inputline ; while ((inputline = bufferreader.readline()) != null) { serverres.append(inputline); } print_log(serverres.tostring()); response_upload_complete(serverres.tostring()); }else { response_error(); } } catch (malformedurlexception e) { e.printstacktrace(); print_error(" malformedurlexception. "); response_error(); } catch (ioexception e) { e.printstacktrace(); print_error(" ioexception. may no internet connection "); response_error(); } { if (httpurlconnection != null) { httpurlconnection.disconnect(); } if (dataoutputstream != null) { try { dataoutputstream.flush(); dataoutputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } } now call "httpurlconnection.getoutputstream()" throws exception :
03-13 15:50:23.088: w/system.err(27349): java.net.socketexception: sendto failed: econnreset (connection reset peer) 03-13 15:50:23.088: w/system.err(27349): @ libcore.io.iobridge.maybethrowaftersendto(iobridge.java:506) 03-13 15:50:23.088: w/system.err(27349): @ libcore.io.iobridge.sendto(iobridge.java:475) 03-13 15:50:23.088: w/system.err(27349): @ java.net.plainsocketimpl.write(plainsocketimpl.java:507) 03-13 15:50:23.088: w/system.err(27349): @ java.net.plainsocketimpl.access$100(plainsocketimpl.java:46) 03-13 15:50:23.093: w/system.err(27349): @ java.net.plainsocketimpl$plainsocketoutputstream.write(plainsocketimpl.java:269) 03-13 15:50:23.093: w/system.err(27349): @ java.io.outputstream.write(outputstream.java:82) 03-13 15:50:23.093: w/system.err(27349): @ libcore.net.http.httpengine.writerequestheaders(httpengine.java:646) 03-13 15:50:23.093: w/system.err(27349): @ libcore.net.http.httpengine.initrequestbodyout(httpengine.java:346) 03-13 15:50:23.093: w/system.err(27349): @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:301) 03-13 15:50:23.093: w/system.err(27349): @ libcore.net.http.httpengine.sendrequest(httpengine.java:239) 03-13 15:50:23.093: w/system.err(27349): @ libcore.net.http.httpurlconnectionimpl.connect(httpurlconnectionimpl.java:80) 03-13 15:50:23.093: w/system.err(27349): @ com.karaoke.service.uploadservice.onhandleintent(uploadservice.java:145) 03-13 15:50:23.093: w/system.err(27349): @ android.app.intentservice$servicehandler.handlemessage(intentservice.java:65) 03-13 15:50:23.093: w/system.err(27349): @ android.os.handler.dispatchmessage(handler.java:99) 03-13 15:50:23.093: w/system.err(27349): @ android.os.looper.loop(looper.java:137) 03-13 15:50:23.093: w/system.err(27349): @ android.os.handlerthread.run(handlerthread.java:60) 03-13 15:50:23.098: w/system.err(27349): caused by: libcore.io.errnoexception: sendto failed: econnreset (connection reset peer) 03-13 15:50:23.098: w/system.err(27349): @ libcore.io.posix.sendtobytes(native method) 03-13 15:50:23.098: w/system.err(27349): @ libcore.io.posix.sendto(posix.java:146) 03-13 15:50:23.098: w/system.err(27349): @ libcore.io.blockguardos.sendto(blockguardos.java:177) 03-13 15:50:23.098: w/system.err(27349): @ libcore.io.iobridge.sendto(iobridge.java:473)
most of time happens first time when start service upload. occurrence random. once exception thrown, if start service again same parameter works fine. occurrence of exception increased bigger files. have noticed issues occurs more in samsung devices, have tried htc evo 3d, , nexus 5 also. can't seems find reason such issue. cant find similar issue on web .
update: don't know why error appearing when switched base url domain name url ip based url worked fine time. still love have explanation on possible reason why happening.
httpurlconnection.disconnect(); httpurlconnection.connect(); i have precisely 0 idea why on earth calling both these methods, suggest removing both these lines of code resolve problem.
Comments
Post a Comment