--------------------------C# Codes Begin------------------------------
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Da
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")] //关键,此为被调用的命名空间
//[SoapRpcService]//指定使用rpc方式
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public bool CheckUserLogin(string sUserName,string sPassWord,string sYardCode)
{
bool bResult = false;
if (UserCommon.CheckUserLogin(sUserName, sPassWord, false))
{
bResult = true;
}
return bResult;
}
--------------------------C# Codes End------------------------------
--------------------------Android Codes Begin------------------------------
package pkg.HelloWorld;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private TextView hintInfo=null;
private EditText edtUser=null;
private EditText edtPwd=null;
private Button confirmbtn=null;
private Button closeButton=null;
@Override
public void on
super.on
setContentView(R.layout.main);
edtUser=(EditText)findViewById(R.id.edtUserName);
edtPwd =(EditText)findViewById(R.id.edtPassWord);
hintInfo=(TextView)findViewById(R.id.hitInfo);
confirmbtn=(Button)findViewById(R.id.btnLogin);
confirmbtn.setOnClickListener(new myClick());
closeButton=(Button)findViewById(R.id.btnClose);
closeButton.setOnClickListener(new CloseClick());
}
private class myClick implements On
@Override
public void on
{
try
{
//hintInfo.setText("进入了事件!");
// TODO Auto-generated method stub
if (edtUser.getText().toString().length()==0)
{
hintInfo.setText("请输入账号 !");
edtUser.selectAll();
return;
}
else if (edtPwd.getText().toString().length()==0)
{
hintInfo.setText("请输入密码 !");
edtUser.selectAll();
return;
}
//调用.net 的WebService.
String nameSpace = "http://tempuri.org/";
String methodName = "CheckUserLogin";
String url ="http://10.0.2.2:1205/SmartYmsWebService/Service.asmx"; //10.0.2.2 为Android模拟器的本地(localhost)IP
String soapAction = nameSpace + methodName;
//设置连接参数
SoapObject request = new SoapObject(nameSpace, methodName);
//增加属性参数。 将相关的函数参数放入到过程中。
request.addProperty("sUserName", edtUser.getText().toString());
request.addProperty("sPassWord", edtPwd.getText().toString());
request.addProperty("sYardCode", "WLY");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//soap协议版本必须用SoapEnvelope.VER11(Soap V1.1);
//注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false
envelope.dotNet = true;
//envelope.setOutputSoapObject(request);//设置请求参数
envelope.bodyOut=request; //enveloper.bodyOut=request 与 envelope.setOutputSoapObject(request) 效果相同。
//step4 创建HttpTransportSE对象
HttpTransportSE ht = new HttpTransportSE(url);
//step5 调用WebService
ht.call(soapAction, envelope); //关键的一步,很多问题都在这一步调试时出现问题。要么是IIS有问题,要么是ksoap2相关参数没配置好。
if(envelope.getResponse()!=null){
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Boolean result =Boolean.parseBoolean(response.toString()) ;
if (result)
{
hintInfo.setText("登录成功!");
}
else
{
hintInfo.setText("登录失败!");
}
}
else {
hintInfo.setText("服务器可能没有开启!");
}
} catch (Exception e)
{
// TODO: handle exception
hintInfo.setText("异常了,原因:" + e.getMessage());
}
}
}
private class CloseClick implements On
@Override
public void on
{
// TODO Auto-generated method stub
System.exit(0);
}
}
--------------------------Android Codes End ------------------------------
另外还有一个重要的环节,就是ksoap2的jar包引用问题:(如图引用不会出现问题否则会提示找不到ksoap2的相关对象错误)