title: "connection refused" error when calling Servlet running in localhost Tomcat from Android app
body: <p>I am trying to access a <strong>Servlet</strong> method through my <strong>Android app</strong>.
  The Servlet is running on <strong>localhost</strong>, on a <strong>Tomcat v8</strong> server.
  When I use the <strong>Emulator</strong>, it works just <strong>fine</strong>.
  But when I am using a real device, i am getting a <strong><code>Connection to http://10.0.2.2:8080 refused</code></strong> error.
  The android device is connected to my laptop, where Tomcat runs.</p>
  
  <p>I propably have to <strong>configure tomcat to allow incoming connections</strong> from another device (my android device) in the same network? (I don't know how)</p>
  
  <p>Android code:</p>
  
  <pre><code>HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost("http://10.0.2.2:8080/DistributionLogisticsOptimizer/Login");
          String responseText = "";
          try {
              // Add your data
              List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
              nameValuePairs.add(new BasicNameValuePair("email",mEmail));
              nameValuePairs.add(new BasicNameValuePair("password", mPassword));
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              /* execute */
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity rp = response.getEntity();
              responseText=readContent(response);
              } 
          catch (ClientProtocolException e) {
                      // TODO Auto-generated catch block
          } 
          catch (IOException e) {
                      e.printStackTrace();
          }
          return responseText;
  </code></pre>
  
  <p>I have also given internet permission to the android manifest</p>
  
  <pre><code>&lt;uses-sdk
  android:minSdkVersion="14"
  android:targetSdkVersion="19" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p>This is the Tomcat configuration</p>
  
  <pre><code>&lt;Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/&gt;
  
  &lt;Engine defaultHost="localhost" name="Catalina"&gt;
    &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;
      &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/&gt;
    &lt;/Realm&gt;
  
    &lt;Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"&gt;
      &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" prefix="localhost_access_log" suffix=".txt"/&gt;
  
    &lt;Context docBase="DistributionLogisticsOptimizer" path="/DistributionLogisticsOptimizer" reloadable="true" source="org.eclipse.jst.jee.server:DistributionLogisticsOptimizer"/&gt;&lt;/Host&gt;
  &lt;/Engine&gt;
  </code></pre>