android - Can not establish HttpUrlConnection -
i coded download of 1gb file in chunks via httpurlconnection. server personal pc apache server, i'm downloading file on galaxy sii android 4.1.2. download using downloadmanager works fine when i'm trying use httpurlconnection i'm getting exception. here code:
try{ uri uri = uri.parse("http://192.168.1.110/1000mb.test"); url url = new url( uri.tostring()); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setreadtimeout(10000 /* milliseconds */); urlconnection.setconnecttimeout(15000 /* milliseconds */); urlconnection.setchunkedstreamingmode(104857600); urlconnection.connect(); file sdcard = environment.getexternalstoragedirectory(); file file = new file(sdcard, "filename.ext"); fileoutputstream fileoutput = new fileoutputstream(file); inputstream inputstream = urlconnection.getinputstream(); byte[] buffer = new byte[1024]; int bufferlength = 0; while ( (bufferlength = inputstream.read(buffer)) > 0 ) { fileoutput.write(buffer, 0, bufferlength); } fileoutput.close(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
and exception get:
e/androidruntime: fatal exception: main java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:275) @ android.view.view.performclick(view.java:4232) @ android.view.view$performclick.run(view.java:17298) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4921) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) @ dalvik.system.nativestart.main(native method) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:270) @ android.view.view.performclick(view.java:4232) @ android.view.view$performclick.run(view.java:17298) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4921) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) @ dalvik.system.nativestart.main(native method) caused by: android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118) @ libcore.io.blockguardos.connect(blockguardos.java:84) @ libcore.io.iobridge.connecterrno(iobridge.java:144) @ libcore.io.iobridge.connect(iobridge.java:112) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) @ java.net.socket.connect(socket.java:842) @ libcore.net.http.httpconnection.<init>(httpconnection.java:76) @ libcore.net.http.httpconnection.<init>(httpconnection.java:50) @ libcore.net.http.httpconnection$address.connect(httpconnection.java:340) @ libcore.net.http.httpconnectionpool.get(httpconnectionpool.java:87) @ libcore.net.http.httpconnection.connect(httpconnection.java:128) @ libcore.net.http.httpengine.opensocketconnection(httpengine.java:315) @ libcore.net.http.httpengine.connect(httpengine.java:310) @ libcore.net.http.httpengine.sendsocketrequest(httpengine.java:289) @ libcore.net.http.httpengine.sendrequest(httpengine.java:239) @ libcore.net.http.httpurlconnectionimpl.connect(httpurlconnectionimpl.java:80) @ com.example.mateusz_rzepczyk.wifitester.mainactivity.addlisteneronbutton1000(mainactivity.java:383) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:270) @ android.view.view.performclick(view.java:4232) @ android.view.view$performclick.run(view.java:17298) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4921) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) @ dalvik.system.nativestart.main(native method)
what noticed: 1) no matter value of connection time out set i'll exception 2) urlconnection.connected false 3) urlconnection response code = -1
what need achieve: download 1gb file in 100mb chunks , save device storage.
please me topic. i'm complete newbie in java.
Comments
Post a Comment