title: Android HttpUrlConnection connect always failed
body: <p>I develop a program that requires HttpUrlConnection. The problem is httpConn.connect() never success. I have checked if the network connection is available and status is connected before.</p>
  
  <pre><code>    InputStream in = null;
      int resCode = -1;
  
      try {
          URL url = new URL(urlStr);
          URLConnection urlConn = url.openConnection();
  
          if (!(urlConn instanceof HttpURLConnection)) {
              throw new IOException("URL is not an Http URL");
          }
          HttpURLConnection httpConn = (HttpURLConnection) urlConn;
          httpConn.setAllowUserInteraction(false);
          httpConn.setInstanceFollowRedirects(true);
          httpConn.setRequestMethod("GET");
          httpConn.connect();
          resCode = httpConn.getResponseCode();
  
          if (resCode == HttpURLConnection.HTTP_OK) {
              in = httpConn.getInputStream();
          }*/
      }
      catch (MalformedURLException e) {
          e.printStackTrace();
      }
      catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Adding this following code in onCreate method only prevent the program to force close.</p>
  
  <pre><code>if (android.os.Build.VERSION.SDK_INT &gt; 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
      }
  </code></pre>
  
  <p>I have added these permissions in manifest</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>Why it can happen and how to solve this problem?</p>