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 ...
随机推荐
- 安卓在SQLiteOpenHelper类进行版本升级和降级
一.升级(使用到onUpgrade()方法和onCreate()没有安装过才用到) 简单理一下思路: v1.0 (也就是说第一次使用这软件,没有安装过 所有在onCreate() 方法里写代码) ...
- windows7实用快捷键 分类: windows常用小技巧 2014-04-18 14:34 169人阅读 评论(0) 收藏
几个比较实用的快捷键 windows键:简写成win win+ 方向键上下,可以使当前窗体放大或缩小 win+ 方向键左右,可以使当前窗体悬靠在左边或右边 win+Home 仅保留当前窗体 ...
- App抓包数据包之Paros的安装及使用
1.在应用程序开发过程中,会遇到很多网络访问问题,自己没有能力提供后台服务,这时就可以抓取网络上的数据包,获取数据接口,应用在程序中.下面介绍使用paros抓取网络数据包得步骤. 2.要使用paros ...
- VS 制作安装包小窥
难得忙里偷闲,看到有关VS制作安装包,按下文小试一把,还行,比不上Installshield. 首先在打开 VS2010 > 文件 >新建项目 创建一个安装项目 XXX 在“目 ...
- [Javascript] String method: endsWith() && startsWith()
With endsWith && startsWith, you can easily find out whether the string ends or starts with ...
- C++发送邮件和附件
c++socketnulldelete服务器stream 头文件 /**************************************************************** ...
- git使用介绍
Git简单介绍 参考网址: git使用简介 这个教程推荐使用:git教程 git和svn的差异 git和svn的最大差异在于git是分布式的管理方式而svn是集中式的管理方式.如果不习惯用代码管理工具 ...
- codevs 1997 守卫者的挑战
/* 表示很遗憾.. 开始状态想的没错 就是转移的时候出了问题 自己也想到了数组平移 然而没往下写 与正解擦肩而过…. 然后为了好转移写了个4维的 时间不多了没来得及降维 草草的算算空间就交了… 尼玛 ...
- java 手动清理缓存的方法
有时候会感觉代码如何也查不出问题,可是缓存就是清好几遍了 这个时候就试试手动清理缓存 到你的编译路径下面 E:\java-workspace\wem\work\org\apache\jsp 手动删除你 ...
- c - 折半查找(二分法检索)
#include <stdio.h> #define LEN 10 /* 折半查找(二分法检索). */ int index_of(int *a, int k) { ; ; int m; ...