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 ...
随机推荐
- html中em和px
px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸 ...
- 如何在myeclipse8.5中使用maven
今天搞了一天才搞清楚这个问题,一直导入不了mavendependencies,后来发现是 13-11-23 下午06时47分11秒: Downloading http://mirrors.ibibli ...
- inux 安装中文支持包及中文字符集配置 +i18n
由于某些原因系统安装时未安装中文支持,导致后续应用出现中文方块乱码现象, 解决方法很简单,当然不是重装,只需以下三步即可搞定. .安装中文包: yum -y groupinstall chinese- ...
- java 数据库两种连接方法
package jdbc; import java.sql.*; public class ConnectionDemo2 { public static final String DBDRIVER= ...
- 不用jquery等框架实现ajax无刷新登录
<script type="text/javascript"> window.onload = function () { document.getElementByI ...
- 【转】JAVA的StringBuffer类
[转]JAVA的StringBuffer类 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...
- PHP 实现无限极栏目分类
首先,创建一个DB CREATE TABLE IF NOT EXISTS `class` ( `id` mediumint(6) NOT NULL AUTO_INCREMENT, `title` va ...
- Wpf 数据绑定简介、实例1
简介:1.WPF绑定使用的源属性必须是依赖项属性,这是因为依赖项属性具有内置的更改通知支持,元素绑定表达式使用了Xaml扩展标记, WPF绑定一个控件是使用Binding.ElementName, 绑 ...
- 自定义android精美聊天界面
编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...
- Windows上帝模式
在桌面新建一个文件夹 按F2重命名为 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} 完成