android+soap+.net+webservice
今天下午一直在折腾android通过soap的方式调用.net下面的webservice服务。
代码很简单,可就是调用不起
测试了很多方法,也谷歌和度娘了一大堆网页,都无用,最后国外网站上的一句话帮我解决了:webservice的命名空间最后需要有反斜杠。
贴出我的代码如下:
第一部分,.net framework4.0下面的webservice服务代码
[WebService(Namespace = "http://tmpuri.org/")]
///[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
///[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebServer : System.Web.Services.WebService
{
[WebMethod]
public string CheckAndLogin(string userID, string userPwd)
{
string rtn = string.Empty;
//登录简单验证
BLL.UsersTable bll_Users = new BLL.UsersTable();
using (StreamWriter sw = new StreamWriter("d:\\webservice_ch.txt", true))
{
sw.WriteLine("userID为:" + userID + "; userPwd为:" + userPwd);
}
DataTable dt = bll_Users.GetList("UserLogin='" + userID + "' and UserPwd='" + userPwd + "'").Tables[0];
if (dt.Rows.Count < 1)
{
rtn = "usernotfound";
}
else
{
rtn = dt.Rows[0]["UserName"].ToString();
}
return rtn;
}
}
///[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
///[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebServer : System.Web.Services.WebService
{
[WebMethod]
public string CheckAndLogin(string userID, string userPwd)
{
string rtn = string.Empty;
//登录简单验证
BLL.UsersTable bll_Users = new BLL.UsersTable();
using (StreamWriter sw = new StreamWriter("d:\\webservice_ch.txt", true))
{
sw.WriteLine("userID为:" + userID + "; userPwd为:" + userPwd);
}
DataTable dt = bll_Users.GetList("UserLogin='" + userID + "' and UserPwd='" + userPwd + "'").Tables[0];
if (dt.Rows.Count < 1)
{
rtn = "usernotfound";
}
else
{
rtn = dt.Rows[0]["UserName"].ToString();
}
return rtn;
}
}
请注意第一行代码
[WebService(Namespace = "http://tmpuri.org/")],Namespace的后面需要有/
android上java调用代码如下:
private void soapCheck(){
String nameSpace = "http://tmpuri.org/";
String methodName = "CheckAndLogin";
//String url = "http://192.168.50.148/PDA/WebService/WebServer.asmx";
String url = "http://192.168.1.9/PDA/WebService/WebServer.asmx";
String soapAction = "http://tmpuri.org/CheckAndLogin";
try{
String userID = this.txtName.getText().toString();
String userPwd = this.txtPwd.getText().toString();
//request
SoapObject request = new SoapObject(nameSpace, methodName);
request.addProperty("userID", userID);
request.addProperty("userPwd", userPwd);
//envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//envelope.encodingStyle =
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
//call
HttpTransportSE ht = new HttpTransportSE(url);
ht.call(soapAction, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
String txt = result.toString();
new AlertDialog.Builder(this).setMessage(result.getProperty("CheckAndLoginResult").toString()).create().show();
} catch(Exception ex){
new AlertDialog.Builder(this).setMessage(ex.getMessage()).create().show();
}
}
String nameSpace = "http://tmpuri.org/";
String methodName = "CheckAndLogin";
//String url = "http://192.168.50.148/PDA/WebService/WebServer.asmx";
String url = "http://192.168.1.9/PDA/WebService/WebServer.asmx";
String soapAction = "http://tmpuri.org/CheckAndLogin";
try{
String userID = this.txtName.getText().toString();
String userPwd = this.txtPwd.getText().toString();
//request
SoapObject request = new SoapObject(nameSpace, methodName);
request.addProperty("userID", userID);
request.addProperty("userPwd", userPwd);
//envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//envelope.encodingStyle =
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
//call
HttpTransportSE ht = new HttpTransportSE(url);
ht.call(soapAction, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
String txt = result.toString();
new AlertDialog.Builder(this).setMessage(result.getProperty("CheckAndLoginResult").toString()).create().show();
} catch(Exception ex){
new AlertDialog.Builder(this).setMessage(ex.getMessage()).create().show();
}
}