我的应用程序使用ksoap2连接到Web服务。该应用程序使用户能够更改提供Web服务的服务器的IP地址/ URL(在设置中)。
问题是,当用户将IP地址/ URL更改为无效位置时,应用程序会挂起很长时间,而LogCat会显示应用程序无法连接到提供的套接字描述。
我该如何解决这个问题?如果找不到Web服务,如何停止应用程序挂起?
如果找不到网络服务,我希望应用用户界面保持原样。有没有办法避免挂起申请?
这是我的代码。请建议任何避免申请悬挂的方法。
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 :(得分:1)
将 ConnectionTimeOut 设置为HttpTransportSE
对象..
使用最新使用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();
}