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;
} }