我的應用程序使用ksoap2連接到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();
}
}
使用connectionTimeOut與Web服務調用。 – user370305
您能否提供代碼片段? – Swayam
這段代碼是否在單獨的線程中運行? –