简体   繁体   English

如何检查SOAP Web服务连接

[英]How to check SOAP webservice connectivity

I'm developing an android application using SOAP webservice. 我正在使用SOAP Web服务开发一个android应用程序。 In this app, I need check the connectivity of the webservice, because if the connection is available i have to show the data in online mode and if the connection is not available i have to show the data in offline mode ie. 在这个应用程序中,我需要检查Web服务的连通性,因为如果连接可用,我必须以在线模式显示数据,如果连接不可用,我必须以离线模式显示数据,即。 I'm using SQLlite database to store the data from the webservice 我正在使用SQLlite数据库存储Web服务中的数据

How can my application test for availability of the server, to control whether the application runs in online or offline mode? 我的应用程序如何测试服务器的可用性,以控制应用程序以联机还是脱机模式运行?

At design time you can point a web browser at the webservice and examine the response. 在设计时,您可以将Web浏览器指向Web服务并检查响应。 It you don't get a response, or not what you expect, you can get more detail using Fiddler 如果您没有得到回应,或者没有您的期望,则可以使用Fiddler获得更多详细信息

At run-time you can simply attempt to access the server in a try/catch exception handler. 在运行时,您可以简单地尝试在try / catch异常处理程序中访问服务器。 Define a public bool IsOnline on something that's visible with appropriate scope, and set it true after successful execution and set it false in the exception handler. 在适当范围内可见的对象上定义一个公共布尔IsOnline,并在成功执行后将其设置为true,并在异常处理程序中将其设置为false。 It's been decades since I used Java but this is the same approach you would use in practically any OO semiprocedural language, like C# or C++ or Java or for that matter VB. 自从我使用Java已有几十年了,但是这与您几乎在任何OO半过程语言(例如C#或C ++或Java或VB)中使用的方法相同。

For this , I followed this method 为此,我遵循了这种方法

  1. I create a Pogo Function (web-method) in my web-service (server) to to check connectivity 我在网络服务(服务器)中创建一个Pogo功能(网络方法)以检查连接性

  2. Create a method to in client side to call the web-method . 在客户端创建一个方法来调用web方法。 While i Enter into the app for first time ,I call this client method from the phone using client function .Then wait for the response.The client method should be surrounded by try-catch block 当我第一次进入应用程序时,我使用客户端功能从电话中调用此客户端方法。然后等待响应。客户端方法应被try-catch块包围

  3. If any exception or timed out happened the response will be null and go to exception stage.So buy checking the response . 如果发生任何异常或超时,则响应将为null并进入异常阶段。因此,请检查响应。 I set a flag (boolean or integer variable) on base of the state of response (null or not null) to decide off-line or on-line mode 我根据响应状态(空或非空)设置一个标志(布尔值或整数变量) ,以决定离线或在线模式

Example with code 代码示例

    public class CallSOAP {

private String SOAP_ACTION="your action url";
private String OPERATION_NAME="";//The web method OR web service you are going to call
private final String WSDL_TARGET_NAMESPACE=SOAP_ACTION;
private  final String SOAP_ADDRESS=Configuration.WEB_SERVICE_URL;//ip-address of serever where you host your service 
private Exception exception;
private String ErrorMsg="";
private String TERST_URL="" ;
public Object call(String MethodeName,ArrayList<PropertyInfo> Parameters){
  this.OPERATION_NAME=MethodeName;
  SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
 if(Parameters != null && Parameters.size()>0){
   for(PropertyInfo propertyInfo : Parameters){
      request.addProperty(propertyInfo);
 }
}

SoapSerializationEnvelope  envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet= true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport =null;
if(!this.TERST_URL.equals(""))
  httpTransport = new HttpTransportSE(this.TERST_URL);
else
  httpTransport = new HttpTransportSE(SOAP_ADDRESS);
 Object Response =null;
 try{
    httpTransport.call(SOAP_ACTION+OPERATION_NAME, envelope);
    Response=envelope.getResponse();
  }
 catch (SocketTimeoutException ex) {
     this.ErrorMsg="Unable to connect";
      Response=null;
     this.exception=ex;
  }
  catch (IOException ie) {
   this.ErrorMsg="Unable to connect";
  Response=null;
   this.exception=ie;
  }
   catch (Exception e) {
    this.ErrorMsg="Unable to connect";
    Response=null;
    this.exception=e;
 }
      return Response;
   }
}

In the code ErrorMsg="Unable to connect"; 在代码中ErrorMsg="Unable to connect"; shows the connection is not available now .So i set the response to null and you can pass this response to activity and check it over there 显示连接现在不可用。因此我将响应设置为null,然后可以将此响应传递给活动并在那边检查它

Refer this 推荐这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM