Android调用Asp.net Web Service示例
WebService代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; namespace NetWS
{
/// <summary>
/// ADWS 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class ADWS : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string GetDate(int t)
{
string result = string.Empty;
switch (t)
{
case :
result = DateTime.Now.ToString("yyyy-MM-dd");
break;
case :
result = DateTime.Now.ToShortDateString();
break;
default:
result = DateTime.Now.ToLongDateString();
break;
}
return result;
} [WebMethod]
public Person GetPerson(string name, bool gender)
{
var p = new Person
{
Name = string.Format("{0}{1}", name, gender ? "先生" : "小姐"),
Gender = gender,
LogTime = DateTime.Now,
Age = new Random().Next(, ),
};
return p;
} public class Person
{
public string Name { get; set; }
public bool Gender { get; set; }
public DateTime LogTime { get; set; }
public int Age { get; set; }
}
}
}
Android调用代码:
package com.example.ws; import java.io.IOException; import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import org.ksoap2.serialization.SoapPrimitive; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.view.*;
import java.text.*;
import java.util.*; public class MainActivity extends Activity {
final static String SERVICE_NS = "http://tempuri.org/";
final static String SERVICE_URL = "http://192.168.0.102:7020/adws.asmx";
private TextView tvShow;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
tvShow.setText(msg.obj.toString());
break;
}
}
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShow = (TextView) findViewById(R.id.tvShow);
} public void singleCall(View source)
{
// 调用的方法
final String methodName = "GetDate";
// 创建HttpTransportSE传输对象
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); // ①
ht.debug = true;
// 使用SOAP1.1协议创建Envelop对象
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // ②
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName); // ③
soapObject.addProperty("t", 1); // ④
// 将soapObject对象设置为 SoapSerializationEnvelope对象的传出SOAP消息
envelope.bodyOut = soapObject; // ⑤
envelope.dotNet = true;
new Thread() {
public void run() {
try {
// 调用Web Service
ht.call(SERVICE_NS + methodName, envelope); // ⑥
if (envelope.getResponse() != null) {
SoapPrimitive soapPrimitive = (SoapPrimitive) envelope.getResponse();
String result = soapPrimitive.toString();
Message msg = new Message();
msg.what = 0x123;
msg.obj = result;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}.start();
} public void complexCall(View source)
{
// 调用的方法
final String methodName = "GetPerson";
// 创建HttpTransportSE传输对象
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); // ①
ht.debug = true;
// 使用SOAP1.1协议创建Envelop对象
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); // ②
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName); // ③
soapObject.addProperty("name", "张三"); // ④
soapObject.addProperty("gender",false);
// 将soapObject对象设置为 SoapSerializationEnvelope对象的传出SOAP消息
envelope.bodyOut = soapObject; // ⑤
envelope.dotNet = true;
new Thread() {
public void run() {
try {
// 调用Web Service
ht.call(SERVICE_NS + methodName, envelope); // ⑥
if (envelope.getResponse() != null) {
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn; //⑦
// 接下来就是从SoapObject对象中解析响应数据的过程了。
SoapObject person = (SoapObject) result.getProperty(0); StringBuilder info = new StringBuilder();
info.append("姓名:");
info.append(person.getProperty("Name"));
info.append("\n性别:");
info.append(person.getProperty("Gender").toString()=="true"?"男":"女");
info.append("\n上次登录时间:");
info.append(person.getProperty("LogTime").toString());
info.append("\n年龄:");
info.append(person.getProperty("Age")); Message msg = new Message();
msg.what = 0x123;
msg.obj = info;
handler.sendMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}.start();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
Android调用Asp.net Web Service示例的更多相关文章
- ASP.NET Web Service如何工作(2)
ASP.NET Web Service如何工作(2) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] HTTP管道一旦调用了.asmx句柄,便 ...
- MVC项目实践,在三层架构下实现SportsStore-09,ASP.NET MVC调用ASP.NET Web API的查询服务
ASP.NET Web API和WCF都体现了REST软件架构风格.在REST中,把一切数据视为资源,所以也是一种面向资源的架构风格.所有的资源都可以通过URI来唯一标识,通过对资源的HTTP操作(G ...
- 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service
在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...
- ASP.NET Web Service如何工作(3)
ASP.NET Web Service如何工作(3) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] 为了使.asmx句柄有可能反串行化SOA ...
- ASP.NET Web Service如何工作(1)
ASP.NET Web Service如何工作(1) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] Summary ASP.NET Web ...
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含 ...
- ASP.NET WEB SERVICE 创建、部署与使用
PS: 开发工具 VS2010, 所有工程都为Debug状态,本人刚接触 Web Service,此文为菜鸟入门用例,高手勿笑! 转载请注明出处 :http://www.cnblogs.com/yyc ...
- Visual Studio 2010中创建ASP.Net Web Service
转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...
- (转)在 Visual Studio 2010 中创建 ASP.Net Web Service
很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net Web Service”这种project了,下面跟帖者云云,有的说这是因为微软已经将Web Service整合进W ...
随机推荐
- Redis sort命令
http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135921.html 1.添加 投票选项到 redis的 List 和HashMap lis ...
- Jemter
1.我们需要创建批量数据 2.jemter连接数据库 3.调用外部数据 4.我要获取的值原本服务器返回的结果是:以下是左边界和右边界.提取想要的数值
- JSP执行过程详解
复习JSP的概念 JSP是Java Server Page的缩写,在传统的HTML页面中加入JSP标签和java的程序片段就构成了JSP. JSP的基本语法:两种注释类型.三个脚本元素.三个元素指令. ...
- STL——空间配置器(构造和析构基本工具)
以STL的运用角度而言,空间配置器是最不需要介绍的东西,它总是隐藏在一切组件(更具体地说是指容器,container)的背后,默默工作,默默付出.但若以STL的实现角度而言,第一个需要介绍的就是空间配 ...
- spring mvc DispatcherServlet详解之一---处理请求深入解析
要深入理解spring mvc的工作流程,就需要先了解spring mvc的架构: 从上图可以看到 前端控制器DispatcherServlet在其中起着主导作用,理解了DispatcherServl ...
- iOS平台基于ffmpeg的视频直播技术揭秘
现在非常流行直播,相信很多人都跟我一样十分好奇这个技术是如何实现的,正好最近在做一个ffmpeg的项目,发现这个工具很容易就可以做直播,下面来给大家分享下技术要点: 首先你得编译出ffmpeg运行所需 ...
- MVC实现登录,增删改查之数据展示:JSP的EL表达式(二)
这里的数据展示利用jsp的EL表达式,后台放入session,前台EL获取 数据库设计是这样的,一个老师对应有多个学生,在学生表student中建立外键tid与老师表teacher的tid对应,现在老 ...
- instancetype vs id for Objective-C
instancetype: 使用 instancetype 编译器和IDE 会做类型检查,而id不会做完整的类型检查. A method with a related result type can ...
- http方法
http method(方法):1.get 从服务器获取资源2.post 向服务器发送资源3.put 向服务器推送资源4.delete 告诉服务器删除某个资源5.head 告诉服务器返回数据时不需要返 ...
- Java对象序列化入门
Java对象序列化入门 关于Java序列化的文章早已是汗牛充栋了,本文是对我个人过往学习,理解及应用Java序列化的一个总结.此文内容涉及Java序列化的基本原理,以及多种方法对序列化形式进行定制 ...