如何防止Android中可怕的KSOAP2连接超时

时间:2012-04-13 12:12:12

标签: android web-services android-ksoap2 connection-timeout

我一直试图阻止KSOAP2调用在通过Android从我的Axis2 Web服务请求数据时超时但是无法这样做。

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,并试图设置超时限制,但到目前为止我还没有成功。所以提出我的问题:

是否还有其他人遇到此问题,如果有,他们是如何解决的?

1 个答案:

答案 0 :(得分:0)

超时启动时会抛出SocketTimeoutException。

你应该在try-catch中捕获它并按你喜欢的方式处理超时