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 ...
随机推荐
- AABB包围盒、OBB包围盒、包围球的比較
1) AABB 包围盒: AABB 包围盒是与坐标轴对齐的包围盒, 简单性好, 紧密性较差(尤其对斜对角方向放置的瘦长形对象, 採用AABB, 将留下非常大的边角空隙, 导致大量不是必需的包围盒相交測 ...
- Firemonkey的旁门左道[四]
做开发,就是发现问题,解决问题,又发现问题...周而复始的循环 下面又应该是Firemonkey下的bug. 官方文档中描述: Using the FireMonkey TMenuBar's OSMe ...
- Hadoop HDFS (3) JAVA訪问HDFS
如今我们来深入了解一下Hadoop的FileSystem类. 这个类是用来跟Hadoop的文件系统进行交互的.尽管我们这里主要是针对HDFS.可是我们还是应该让我们的代码仅仅使用抽象类FileSyst ...
- [AngularJS] 5 simple ways to speed up your AngularJS application
Nowdays, Single page apps are becoming increasingly popular among the fornt-end developers. It is th ...
- [Angular 2] Exposing component properties to the template
Showing you how you can expose properties on your Controller to access them using #refs inside of yo ...
- 深入分析 Java 中的中文编码问题--转
几种常见的编码格式 为什么要编码 不知道大家有没有想过一个问题,那就是为什么要编码?我们能不能不编码?要回答这个问题必须要回到计算机是如何表示我们人类能够理解的符号的,这些符号也就是我们人类使用的语言 ...
- Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)
主要通过重写 onInterceptTouchEvent 事件来解决,代码如下: package com.cm.android.pad.view.itemView; import android.co ...
- Linux磁盘管理:LVM逻辑卷的创建及使用
一.创建LVM逻辑卷 事先添加了三块虚拟物理磁盘/dev/sdb 1G, /dev/sdc 2G, /dev/sdd 3G 使用fdisk –l命令查看: [root@localhost ~]# fd ...
- 9.16noip模拟试题
题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的 ...
- asp.net中@page 指令的属性Inherits、Src、CodeBehind区别
在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. <%@ Page language="c#" C ...