2011-07-13 30 views
-1

我的要求是,两个不同类型的警报消息的需要指定 Android应用程序正在访问wcf服务,如何识别WCF服务工作或没有使用Android

  1. 如果WCF服务无法正常工作。我要告诉消息"Service is not working"
  2. 服务工作正常,但访问时间长则需要告诉"Timeout to connect to service."

第二个选项我已经做了。我们如何才能找到服务正在工作与否?有可能检查吗?

回答

1

1. if WCF service is not working, . I have to tell the message "Service is not working "

确保您的HTTP请求的类检查的404错误代码,如果错误代码是404,那么你可以告诉用户该服务已关闭。

2. Service is working fine, but accessing time is long then need to tell "Timeout to connect to service."

您可以通过两种方式做到这一点,首先,有一个TimerTask或延迟处理所需超时后运行,当然后取消请求,并显示超时错误。另一种更好的方法是使用setConnectionTimeout()和setSoTimeout()来设置请求本身的超时值,ontimeout会产生一个ConnectTimeoutException,您可以捕获它以显示该消息。

HttpParams httpParameters = new BasicHttpParams(); 
// Set the timeout in milliseconds until a connection is established. 
int timeoutConnection = 20000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, 
timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
int timeoutSocket = 60000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
HttpClient client = new DefaultHttpClient(httpParameters); 
+0

谢谢你的回答。第二个选项确定。在第一个选项中,我们如何获取错误代码?我正在使用Ksoap。 SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);数据转化为肥皂 envelope.dotNet = true; envelope.setOutputSoapObject(request); AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL); try { httpTransport.call(SOAP_ACTION,envelope); responses =(SoapPrimitive)envelope.getResponse(); } catch(Exception e){} – Piraba

0

当您尝试连接不工作的服务时,您将收到指示服务器关闭的异常。

你想要特别的东西吗?

+0

目前我正在加强我的android应用程序。我没有启动我的wcf服务,那个时候android应用程序访问wcf服务,它应该告诉“服务不工作/关闭”。这是我的场景。 – Piraba

+0

如果是这样,它应该很简单,您只需要在尝试建立与服务的连接时捕获异常;它会在出现异常时显示警报消息。 – Jack