Android调用C#写的WebService

学习自:

http://www.cnblogs.com/kissazi2/p/3406662.html

运行环境

Win10

VS 2015

Android Studio 2.2.3

KSOAP2的Jar包

Overview

众所周知,符合W3C标准的,HTTP协议、SOAP 协议等,是没有语言和平台的限制的。因为实际需要我们可能有时候要调用C#编写的WebService,这里我们需要添加一个Jar包的依赖。

[KSOAP2点击下载](http://pan.baidu.com/s/1skKjvYX)

需要了解的一些信息

我们的WebService,建立以后,这么一个地址用来存放我们WebService的一些描述的信息。http://localhost:54603/NBAWebService.asmx?wsdl

需要了解的信息

  1. WebService 命名空间
  2. 要执行的方法名
  3. SOAPAction
  4. SOAP 的版本.

Note:

  • 因为涉及到了网络,一定要在子线程中使用。
  • 访问网络需要加上网络权限。
  • 传递参数的时候,参数的名称和顺序不能乱掉。

WebService的代码

我们将以下面的HelloWorld方法为例子

namespace NBAWebService
{
/// <summary>
/// Summary description for NBAWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class NBAWebService : System.Web.Services.WebService
{ public MyLinqDataContext mldc = MethodHelper.GetLinq(); [WebMethod]
public string HelloWorld()
{
return "Hello World";
} //****************************
}
}

Android端调用代码


/*
* 用来调用C#的WebService
* */
public class ConnectionWebService {
//WebService的地址
//10.0.2.2 该地址是我们,android模拟器访问本机时使用的IP地址
static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx";
//WebService 的命名空间
static final String NAMESPACE = "http://tempuri.org/"; /*
* 调用WebService的方法
* */
public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException {
/*
* SOAP的对象
* namespace: 调用的命名空间
* method name = 调用的方法名
* */
SoapObject soapObject = new SoapObject(NAMESPACE, methodName); if (parameterMap != null) {
//给SOAP对象传入我们需要的参数
for (Map.Entry<String, String> item : parameterMap.entrySet()) {
soapObject.addProperty(item.getKey(), item.getValue());
}
} //设置我们的 WebService的SOAP版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.bodyOut = soapObject;
//与.net兼容
envelope.dotNet = true; //调用方法
HttpTransportSE httpTransportSE = new HttpTransportSE(URI); httpTransportSE.call(NAMESPACE + "/" + methodName, envelope); //获取到返回值
SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
return sp.toString();
}
}
分析

这是我们调用这个WebService是使用的基本上不会改变的参数

	 //WebService的地址
//10.0.2.2 该地址是我们,android模拟器访问本机时使用的IP地址
static final String URI = "http://10.0.2.2:8088/NBAWebService.asmx";
//WebService 的命名空间
static final String NAMESPACE = "http://tempuri.org/";

我们方法传递的参数

  • methodName 需要调用的WebService的方法名
  • parameterMap 方法所需要的参数 格式为 key = 参数的名称 value = 参数的值
public static String callMethod(String methodName, Map<String, String> parameterMap) throws IOException, XmlPullParserException

指定WebService命名控件和需要调用的方法

/*
* SOAP的对象
* namespace: 调用的命名空间
* method name = 调用的方法名
* */
SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

添加参数, parameterMap 为null 那么就表明该方法不需要参数。

if (parameterMap != null) {
//给SOAP对象传入我们需要的参数
for (Map.Entry<String, String> item : parameterMap.entrySet()) {
soapObject.addProperty(item.getKey(), item.getValue());
}
}

SOAP的请求信息,该信息是由SoapSerializationEnvelope 对象描述的。

//设置我们的 WebService的SOAP版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); envelope.bodyOut = soapObject;
//与.net兼容
envelope.dotNet = true;

创建HTTPTransportsSE 对象,构造参数中传递,WebService的URL

call 方法调用我们的WebService方法, 传递我们的SOAPAction和我们的SoapSerializationEnvelope 对象

一般来说 SOAPAction 是 命名空间+"/"+方法名.

 //设置我们的 WebService的URI
//调用方法
HttpTransportSE httpTransportSE = new HttpTransportSE(URI);
httpTransportSE.call(NAMESPACE + "/" + methodName, envelope);

获取到返回值。

//获取到返回值
SoapPrimitive sp = (SoapPrimitive) envelope.getResponse();
return sp.toString();
使用
 @Override
public void run() {
try {
String jsonContent = ConnectionWebService.callMethod("HelloWorld", null);
LogHelper.i(jsonContent);
} catch (Exception e) {
LogHelper.i("报错了");
}
}

最新文章

  1. 《Scalable IO in Java》笔记
  2. NET基础(4):引用类型和值类型
  3. Windows 上的 Jetty 小工具
  4. codeforces733-C. Epidemic in Monstropolis 贪心加链表
  5. 定位一组对象-checkbox 、radiobutton
  6. 转:Servlet的url匹配以及url-pattern详解
  7. &lt;%@include和&lt;jsp:include
  8. python列表删除重复元素的三种方法
  9. 【Android - V】之Toolbar的使用
  10. hdu1215 正整数唯一分解定理应用
  11. VIM 用正则表达式
  12. 关于HTTP GET &amp; POST的区别(转)
  13. linux之软件安装
  14. 码农也来关注下经济问题&lt;美元加息&gt;对我们的影响
  15. 浅入深出Vue系列
  16. 基于centos7的真实机环境下安装 vmware workstastion
  17. String和get
  18. ubuntu-Linux下如何安装Tensorflow?
  19. java环境变量配置方法
  20. 深入理解Java面向对象三大特性 封装 继承 多态

热门文章

  1. HDU 4548 美素数 在线打表加数状数组
  2. [Openwrt 扩展下篇] Openwrt搭建私有云Owncloud 9
  3. D. Makoto and a Blackboard(积性函数+DP)
  4. 结构体变量的sizeof计算
  5. 跨站请求伪造(CSRF)攻击原理解析:比你所想的更危险
  6. layui table表格字段过长,展示不完整时,鼠标放到上面展示完整信息
  7. 一步一步搭建oracle 11gR2 rac+dg之database安装(五)【转】
  8. APUE-文件和目录(二)函数access,mask,chmod和粘着位
  9. shell函数-页面跳转练习-&gt;
  10. TypeScript的配置文件 tsconfig.json