当通过 Android 从我的 Axis2 Web 服务请求数据时,我一直试图防止 KSOAP2 调用超时,但一直无法这样做。
Web 服务的背景知识。
我的 Web 服务在 Apache Tomcat 服务器上运行,用于返回站点的实时信息。当我通过浏览器尝试时,它按预期工作,在网站上它也按预期工作。
问题
但是,当我尝试在我的 Android 应用程序中使用相同的方法时,有时我会收到以下堆栈跟踪错误。其他时候请求会成功。有时请求会立即超时,有时它会在超时前等待大约一分钟。
设备
我已经通过三星 Galaxy SII(平台 2.3.3)和三星 Galaxy SI(平台 2.2.2)使用 WiFi 和使用 3G 进行了尝试,结果相同。但是,通过模拟器(平台 2.2)使用它时它永远不会超时。
我的代码
String NAMESPACE = "http://service.domain.ie";
String URL = "http://www.myhost.com/axis2/services/RealTime?wsdl";
String METHOD_NAME = "getStationData";
String SOAP_ACTION = "RealTime";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("stationType", getStationType());
request.addProperty("stationApiCode", getApiCode());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport;
SoapObject response = null;
try {
androidHttpTransport = new HttpTransportSE(URL,60000); // Tried Setting the Timeout to 1 Minute
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION,envelope);
response = (SoapObject)envelope.bodyIn;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
堆栈跟踪错误
04-13 13:07:18.015: W/System.err(9249): java.net.SocketTimeoutException
04-13 13:07:18.015: W/System.err(9249): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:461)
04-13 13:07:18.015: W/System.err(9249): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
04-13 13:07:18.015: W/System.err(9249): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:65)
04-13 13:07:18.015: W/System.err(9249): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:140)
04-13 13:07:18.015: W/System.err(9249): at java.io.BufferedInputStream.read(BufferedInputStream.java:225)
04-13 13:07:18.020: W/System.err(9249): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readLine(HttpURLConnectionImpl.java:660)
04-13 13:07:18.020: W/System.err(9249): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readResponseHeaders(HttpURLConnectionImpl.java:690)
04-13 13:07:18.020: W/System.err(9249): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1040)
04-13 13:07:18.020: W/System.err(9249): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
04-13 13:07:18.020: W/System.err(9249): at org.ksoap2.transport.ServiceConnectionSE.openInputStream(ServiceConnectionSE.java:113)
04-13 13:07:18.020: W/System.err(9249): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:160)
04-13 13:07:18.050: W/System.err(9249): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
04-13 13:07:18.050: W/System.err(9249): at ie.smartcommuter.models.Station.getRealTimeData(Station.java:123)
04-13 13:07:18.050: W/System.err(9249): at ie.smartcommuter.controllers.tabcontents.StationRealtimeActivity$1.run(StationRealtimeActivity.java:95)
04-13 13:07:18.050: W/System.err(9249): at java.lang.Thread.run(Thread.java:1019)
我已将 KSOAP2 从 2.4 更新到 2.5.7 并尝试设置超时限制,但到目前为止我没有成功。所以要回答我的问题:
有没有其他人遇到过这个问题,如果有,他们是如何解决的?