title: Can't access server socket with android app
body: <p>I want to see the exact headers my android app is sending while making a web request so I thought I'd simply create a simple server app in java on my local machine and have my android app make a call to it. Then simply dump the request to the console so I could see what the app is sending. However when I tried to connect, the app hangs and stops responding.</p>
  
  <p>I created a simple server the only accepts a connection and sysouts the data it gets. The server runs fine and if I hit it from a web browser on my computer will print the headers from the web browsers request. So I know the server works fine.</p>
  
  <p>Here's the code from my app:</p>
  
  <pre><code>URL url = new URL("http://192.168.1.11:9000");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setDoOutput(true);
  connection.connect();
  
  PrintWriter writer = new PrintWriter(connection.getOuputStream(), true);
  writer.write("hi");
  writer.close();
  </code></pre>
  
  <p>Simple. I only want the headers after all. Now I started without a post and using:</p>
  
  <pre><code>URL url = new URL("http://192.168.1.11:9000");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
  BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  in.close();
  </code></pre>
  
  <p>but that doesn't work. The app stops responding on the getInputStream() request. It just stops and won't continue. The server gets no connection request either.</p>
  
  <p>So in all, the app is blocking on the url connection's getInputStream and I can't figure out why.</p>
  
  <p>Now I've searched for awhile and found these: </p>
  
  <p><a href="https://stackoverflow.com/questions/8317083/android-app-communicating-with-server-via-sockets">Android app communicating with server via sockets</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4032995/socket-exception-socket-not-connected-android">socket exception socket not connected android</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16568244/android-embedded-browser-cant-connect-to-server-on-lan">Android embedded browser cant connect to server on LAN</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests">Using java.net.URLConnection to fire and handle HTTP requests</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11244571/client-socket-cannot-connect-to-running-socket-server">Client Socket cannot connect to running Socket server</a></p>
  
  <p>But nothing helps. I'm not using the localhost like everyone with this problem seems to be  and I've tried using the androids 10.0.0.2 but that doesnt work either.</p>
  
  <p>I'm not on a network that restricts anything (I'm home) and I've tried using the first set of code shown in order to send a message to my server but not even that works (it runs fine but the server never gets a client. Hows that work?).</p>
  
  <p>I tried using both URLConnection and HttpURLConnection, they both have the same problem.</p>
  
  <p>I'm also using the internet permission in my app, so it does have the permission needed.</p>
  
  <p>I'm at a loss at this point. Why can't I make a simple call to my server?</p>
  
  <p>EDIT</p>
  
  <p>I used the exact code from androids documentation:</p>
  
  <pre><code>private String downloadUrl(String myurl) throws IOException {
      InputStream is = null;
      // Only display the first 500 characters of the retrieved
      // web page content.
      int len = 500;
  
      try {
          URL url = new URL("http://10.0.2.2:9000");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setReadTimeout(10000 /* milliseconds */);
          conn.setConnectTimeout(15000 /* milliseconds */);
          conn.setRequestMethod("GET");
          conn.setDoInput(true);
          // Starts the query
          conn.connect();
          int response = conn.getResponseCode();
          is = conn.getInputStream();
  
          // Convert the InputStream into a string
          String contentAsString = readIt(is, len);
          return contentAsString;
  
          // Makes sure that the InputStream is closed after the app is
          // finished using it.
      } finally {
          if (is != null) {
              is.close();
          }
      }
  }
  </code></pre>
  
  <p>but even that doesn't work. It still hangs. Only now it hangs on the getResponseCode(). Then throws a timeout exception. The server never gets a request though.</p>