title: Can not establish HttpUrlConnection
body: <p>I coded download of 1gb file in chunks via HttpUrlConnection. Server is my personal PC with Apache server, I'm downloading file on Galaxy SII with Android 4.1.2. Download using DownloadManager works fine but when I'm trying to use HttpUrlConnection  I'm getting an exception. Here is my code:</p>
  
  <pre><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)) &gt; 0 ) {
              fileOutput.write(buffer, 0, bufferLength);
          }
          fileOutput.close();
  
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>And the exception I get:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                                           at android.view.View.performClick(View.java:4232)
                                                                                           at android.view.View$PerformClick.run(View.java:17298)
                                                                                           at android.os.Handler.handleCallback(Handler.java:615)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                           at android.os.Looper.loop(Looper.java:137)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:4921)
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                                           at android.view.View.performClick(View.java:4232) 
                                                                                           at android.view.View$PerformClick.run(View.java:17298) 
                                                                                           at android.os.Handler.handleCallback(Handler.java:615) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                                           at android.os.Looper.loop(Looper.java:137) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:4921) 
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                                        Caused by: android.os.NetworkOnMainThreadException
                                                                                           at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
                                                                                           at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
                                                                                           at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
                                                                                           at libcore.io.IoBridge.connect(IoBridge.java:112)
                                                                                           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
                                                                                           at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
                                                                                           at java.net.Socket.connect(Socket.java:842)
                                                                                           at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:76)
                                                                                           at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
                                                                                           at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
                                                                                           at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
                                                                                           at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                                           at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
                                                                                           at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
                                                                                           at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
                                                                                           at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
                                                                                           at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
                                                                                           at com.example.mateusz_rzepczyk.wifitester.MainActivity.addListenerOnButton1000(MainActivity.java:383)
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                                           at android.view.View.performClick(View.java:4232) 
                                                                                           at android.view.View$PerformClick.run(View.java:17298) 
                                                                                           at android.os.Handler.handleCallback(Handler.java:615) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                                           at android.os.Looper.loop(Looper.java:137) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:4921) 
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                                           at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>What I noticed:
  1) no matter what value of connection time out I set I'll always get the exception
  2) urlConnection.connected is always false
  3) urlConnection response code is always = -1</p>
  
  <p>What I need to achieve:
  Download 1gb file in 100mb chunks and save it to device storage.</p>
  
  <p>Please help me with that topic. I'm complete newbie in Java. </p>