2012-04-13 55 views
3

我一直試圖通過Android從Axis2 Web服務請求數據時阻止KSOAP2調用超時,但一直未能這樣做。如何在Android中防止可怕的KSOAP2連接超時

Web服務的背景。

我的Web服務正在Apache Tomcat服務器上運行,用於爲站點返回實時信息。當我嘗試通過瀏覽器時,它按預期工作,並在網站上也按預期工作。

的問題

然而,當我試圖用同樣的方法在我的Android應用程序,有時我會得到下面的堆棧跟蹤誤差。其他時候請求會成功。有時候,請求會立即超時,而其他時間會在超時前等待一分鐘左右。

設備

我走過了三星Galaxy SII(平臺2.3.3),並使用WiFi和3G使用具有相同結果的三星Galaxy SI(平臺2.2.2)試了一下。但是,通過仿真器(Platform 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,並試圖設置超時限制,但我一直不成功如此遠。所以要得到我的問題:

有其他人有這個問題,如果是的話,他們如何解決它?

+0

不得不嘗試超時用'TIMEOUT = 20000;'在'新HttpTransportSE(URL,60000); ' – Herry 2012-04-13 12:24:53

回答

0

當超時啓動時它會拋出SocketTimeoutException。

你應該抓住它在你的try-catch並處理超時,只要你喜歡

+0

請注意,您不捕捉SocketTimeoutException – user2261183 2013-04-28 11:21:19