title: Android App force closes on connection refuse
body: <p>i am trying to create an application which uses rest template for login. Code works fine when tomcat is up and running with application. when i stop tomcat, application trows "connection refuse exception" and application closed. What i want is "how to handle the exception to stop application from getting closed".</p>
  
  <p>On emulator i get message "Unfortunately application is closed"</p>
  
  <p>Here is my code.</p>
  
  <pre><code>public ResponseEntity login(String userName, String password) {
  
          final String url = getString(R.string.tm_user_service_uri)
                  + "/users/authenticate?email=" + userName + "&amp;password="
                  + password;
          HttpAuthentication authHeader = new HttpBasicAuthentication(userName,
                  password);
          HttpHeaders requestHeaders = new HttpHeaders();
          requestHeaders.setAuthorization(authHeader);
          ResponseEntity&lt;Object&gt; response = null;
          HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
  
          // Create a new RestTemplate instance
          RestTemplate restTemplate = new RestTemplate();
  
          // Add the String message converter
          restTemplate.getMessageConverters().add(
                  new StringHttpMessageConverter());
          restTemplate.getMessageConverters().add(
                  new MappingJackson2HttpMessageConverter());
  
          try {
              // Make the HTTP GET request to the Basic Auth protected URL
                      response = restTemplate.exchange(url,
                      HttpMethod.GET, requestEntity, Object.class);
              return response;
          } catch (HttpClientErrorException e) {
              Log.e("Network Error ", e.getLocalizedMessage(), e);
          }
  
          return response;
      }
  </code></pre>
  
  <p>My stack trace is </p>
  
  <pre><code>07-27 15:22:11.880: E/AndroidRuntime(1083): FATAL EXCEPTION: Thread-92
  07-27 15:22:11.880: E/AndroidRuntime(1083): org.springframework.web.client.ResourceAccessException: I/O error: failed to connect to /10.0.2.2 (port 8080): connect failed: ECONNREFUSED (Connection refused); nested exception is java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ECONNREFUSED (Connection refused)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at com.teammatch.android.activities.MainActivity.login(MainActivity.java:144)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at com.teammatch.android.activities.MainActivity.tryLogin(MainActivity.java:71)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at com.teammatch.android.activities.MainActivity$1.run(MainActivity.java:60)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at java.lang.Thread.run(Thread.java:856)
  07-27 15:22:11.880: E/AndroidRuntime(1083): Caused by: java.net.ConnectException: failed to connect to /10.0.2.2 (port 8080): connect failed: ECONNREFUSED (Connection refused)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.io.IoBridge.connect(IoBridge.java:114)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at java.net.Socket.connect(Socket.java:842)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:76)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:72)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:63)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:476)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     ... 6 more
  07-27 15:22:11.880: E/AndroidRuntime(1083): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.io.Posix.connect(Native Method)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     at libcore.io.IoBridge.connect(IoBridge.java:112)
  07-27 15:22:11.880: E/AndroidRuntime(1083):     ... 23 more
  </code></pre>