我在我的android应用程序上使用restTemplate(来自spring android框架)来联系一个带有post的rest服务器,我使用一个AsyncTask来发送请求和两个类Request和Response,它们是以Json形式发送的POJO。
public class RequestSender extends AsyncTask<Object, Void, Response> {
private RestTemplate restTemplate = new RestTemplate();
private static final String SERVER_REQUEST_PATH = "/path/to/rest/service";
@Override
protected Response doInBackground(Object... args) {
String url = (String) args[0] + SERVER_REQUEST_PATH;
Request requestArgs = (Request) args[1];
Response response = null;
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
try {
response = restTemplate.postForObject(url, requestArgs, Response.class);
} catch (RestClientException e) {
e.printStackTrace();
response = null;
}
return response;
}
}
如您所见,代码非常简单,并且大部分时间都可以正常工作,但不是按特定顺序:
- 我向服务器发出第一个(或更多)请求;
- 我重新启动服务器而不重新启动应用程序;
- 我向服务器发出新请求;
然后服务器重启后的第一个请求(步骤 3)不会通过 postForObject(...) 方法发送到服务器,但下一个请求是。这第一个请求给我一个错误:
07-10 10:24:13.402: W/System.err(4827): org.springframework.web.client.ResourceAccessException: I/O error: null; nested exception is java.io.EOFException
07-10 10:24:13.402: W/System.err(4827): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
07-10 10:24:13.402: W/System.err(4827): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
07-10 10:24:13.402: W/System.err(4827): at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:317)
07-10 10:24:13.402: W/System.err(4827): at com.myapp.servercommunication.RequestSender.doInBackground(RequestSender.java:42)
07-10 10:24:13.406: W/System.err(4827): at com.myapp.servercommunication.RequestSender.doInBackground(RequestSender.java:1)
07-10 10:24:13.406: W/System.err(4827): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-10 10:24:13.406: W/System.err(4827): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-10 10:24:13.406: W/System.err(4827): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-10 10:24:13.406: W/System.err(4827): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-10 10:24:13.406: W/System.err(4827): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-10 10:24:13.406: W/System.err(4827): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-10 10:24:13.406: W/System.err(4827): at java.lang.Thread.run(Thread.java:856)
07-10 10:24:13.409: W/System.err(4827): Caused by: java.io.EOFException
07-10 10:24:13.413: W/System.err(4827): at libcore.io.Streams.readAsciiLine(Streams.java:203)
07-10 10:24:13.417: W/System.err(4827): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
07-10 10:24:13.417: W/System.err(4827): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
07-10 10:24:13.417: W/System.err(4827): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
07-10 10:24:13.417: W/System.err(4827): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
07-10 10:24:13.417: W/System.err(4827): at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:54)
07-10 10:24:13.417: W/System.err(4827): at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:80)
07-10 10:24:13.421: W/System.err(4827): at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
07-10 10:24:13.421: W/System.err(4827): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:477)
07-10 10:24:13.421: W/System.
err(4827): ... 11 more
即使用例并不重要,我想知道为什么会抛出这个错误,我的实现是否有问题,或者它是 RestTemplate 的正常行为?