2012-06-20 69 views
0

我的應用程序使用kso​​ap2連接到Web服務。該應用程序使用戶可以更改提供Web服務的服務器的IP地址/ URL(在設置中)。與無效Web服務的Android連接使應用程序掛起

問題是當用戶將IP地址/ URL更改爲無效位置時,應用程序掛起很長時間,LogCat顯示應用程序無法連接到提供的套接字說明。

我該如何解決這個問題?如果Web服務沒有找到,我該如何阻止應用程序掛起?

我希望應用程序UI保持原樣,如果找不到網絡服務。有什麼辦法可以避免應用程序掛起?

這是我的代碼。請提出任何方法以避免掛起應用程序。

private void getControlFromServer() { 
    // TODO Auto-generated method stub 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_CONTROL); 
    SoapSerializationEnvelope envelope = 
     new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 




    try { 
      androidHttpTransport.call(SOAP_ACTION_GET_CONTROL, envelope); 




      SoapObject result=(SoapObject)envelope.getResponse(); //To get the data. 

      tvTemp.setText("Received :" + result.getProperty(0).toString()+ " : " + result.getProperty(1).toString() + " : " + result.getProperty(2).toString() + " : " + result.getProperty(3).toString() + " : " + result.getProperty(4).toString() + " : " + result.getProperty(5).toString() + " : " + result.getProperty(6).toString()); 

    } catch (Exception e) { 


      tvTemp.setText("Error"); 
      e.printStackTrace(); 

     } 


} 
+0

使用connectionTimeOut與Web服務調用。 – user370305

+0

您能否提供代碼片段? – Swayam

+2

這段代碼是否在單獨的線程中運行? –

回答

1

ConnectionTimeOutHttpTransportSE對象..

使用最新的使用ksoap2 API version 2.5.7或更大,

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 
HttpTransportSE androidHttpTransport; 
SoapObject response = null; 

try { 
     androidHttpTransport = new HttpTransportSE(URL,6000); // 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(); 
    } 
+0

謝謝。現在就像魅力一樣。我已經在使用kso​​ap2 2.6.5版。只需將該參數添加到newHttpTansportSE()。 – Swayam

+0

歡迎夥伴..!快樂編碼..! – user370305

相關問題