我正在尝试创建一个使用 rest 模板进行登录的应用程序。当 tomcat 启动并与应用程序一起运行时,代码工作正常。当我停止 tomcat 时,应用程序出现“连接拒绝异常”并且应用程序关闭。我想要的是“如何处理异常以阻止应用程序关闭”。
在模拟器上我收到消息“不幸的是应用程序已关闭”
这是我的代码。
public ResponseEntity login(String userName, String password) {
final String url = getString(R.string.tm_user_service_uri)
+ "/users/authenticate?email=" + userName + "&password="
+ password;
HttpAuthentication authHeader = new HttpBasicAuthentication(userName,
password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
ResponseEntity<Object> response = null;
HttpEntity<?> requestEntity = new HttpEntity<Object>(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;
}
我的堆栈跟踪是
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.<init>(HttpConnection.java:76)
07-27 15:22:11.880: E/AndroidRuntime(1083): at libcore.net.http.HttpConnection.<init>(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