Android使用ksoap2调用C#中的webservice函数方法

来源:互联网 发布:分页查询sql语句mysql 编辑:程序博客网 时间:2024/05/20 12:46

Android使用ksoap2调用C#中的webservice函数方法

一:webService简介

那么什么是webService呢?,它是一种基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。

二:在AdroidManifest.xml中加入权限

   <!-- 访问网络的权限 -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

三:导入ksoap2包

首先下载KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包 复制到工程下的lib文件件里面

然后在android项目:右键->build path(构建路径)->configure build path(添加外部归档)--选择ksoap2

四:编写android可客户端代码

导入包库:

import org.ksoap2.SoapEnvelope;  
import org.ksoap2.serialization.SoapObject;  
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

函数方法:

 try { 
        final String SERVER_URL = "http://124.205.154.198:8081/Service.asmx";
        String nameSpace = "http://tempuri.org/";//命名空间
      String methodName = "Sum";//方法名
      String soapAction = "http://tempuri.org/Sum";//HelloWorld  命名空间/方法名
      //创建SoapObject实例
         SoapObject request = new SoapObject(nameSpace, methodName); 
         request.addProperty("a", "g"); //这个是传递参数的 当然了不要参数就不必写这个了啊
         //request.addProperty("passonString", "Rajapandian"); //这个是传递参数的  
         //生成调用web service方法的soap请求消息
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true; //设置.net web service
         envelope.setOutputSoapObject(request);//发送请求
         HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); 
         androidHttpTransport.call(soapAction, envelope);  
         Object result = (Object) envelope.getResponse(); 
         //textView.setText(e.getMessage());   
         textView.setText(result.toString());
         //textView.setText("7");
         new AlertDialog.Builder(this).setTitle("Hint").setMessage(result.toString()).setPositiveButton("OK", null).show();
         } 
        catch (Exception e) 
        {  
          System.out.println(e.getMessage());  
          textView.setText(e.getMessage()); 
          new AlertDialog.Builder(this).setTitle("Hint").setMessage(e.getMessage()).setPositiveButton("OK", null).show(); 
        } 

五:webService方法

[WebMethod]
    public string Sum(string a)
    {
        
        string c =a+"hello android";
        return c;
    }