我正在为我的应用程序对API发出Post请求,当我在Nexus 5上执行此操作时一切正常但是当我在HTC One X或Samsung Galaxy S3上执行此请求时出现以下错误:< / p>
07-01 11:48:36.605 30101-30251/eu.package.name E/AndroidRuntime﹕ FATAL EXCEPTION: pool-4-thread-3
org.springframework.web.client.ResourceAccessException: I/O error: null; nested exception is java.io.EOFException
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
at eu.package.name.Rest.RESTClient_.signinFacebook(RESTClient_.java:155)
at eu.package.name.Models.User.userLoginFacebook(User.java:91)
at eu.package.name.Models.User_.access$101(User_.java:14)
at eu.package.name.Models.User_$2.execute(User_.java:65)
at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
at com.splunk.mint.network.http.MonitorableHttpURLConnection.getResponseCode(MonitorableHttpURLConnection.java:362)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:54)
at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:80)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:477)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
at eu.package.name.Rest.RESTClient_.signinFacebook(RESTClient_.java:155)
at eu.package.name.Models.User.userLoginFacebook(User.java:91)
at eu.package.name.Models.User_.access$101(User_.java:14)
at eu.package.name.Models.User_$2.execute(User_.java:65)
at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
我正在做的请求是:
@Post("/signin/facebook")
LoginResponse signinFacebook(FacebookLoginRequest facebookLoginRequest);
我已经尝试通过执行以下操作来禁用keep-alive:
restClient.setHeader("Connection", "Close");
LoginResponse loginResponse = restClient.signinFacebook(facebookLoginRequest);
答案 0 :(得分:3)
我建议尝试okhttp客户端:
@Rest(rootUrl = "url", requestFactory = OkHttpClientHttpRequestFactory.class)
这应解决许多与标准Android http客户端相关的问题,包括这个问题。请注意OkHttpClientHttpRequestFactory
仅适用于Spring Android 2.0。
编辑:您必须声明以下依赖项才能使用OkHttpClientHttpRequestFactory
:
compile "org.springframework.android:spring-android-rest-template:2.0.0.M2"
compile "com.squareup.okhttp:okhttp:2.4.0"
compile "com.squareup.okhttp:okhttp-urlconnection:2.4.0"