我创建了一个Web服务(.asmx)。有一个Login方法。它返回一个字符串。
DBClass myDBClass= new DBClass ();
public string ResultUserName{ get; set; }
[WebMethod ]
public string Login(string UserName,string Password)
{
DataTable tb = new DataTable();
string sql = @"Select UserName from UserTable where UserName='"+UserName+"' and Password='"+Password+"' ";
tb = myDBClass.Get(sql, "login");
if (tb.Rows.Count > 0)
{
ResultUserName= Convert.ToString(tb.Rows[0]["UserName"]);
}
else
{
ResultUserName= "The UserName Is Not Found";
}
return ResultUserName;
}
我正在使用Android的ksoap2库。
我为连接服务创建了一个类,并从服务中获取结果。
CallSoap.java
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class CallSoap {
public final String SOAP_ACTION = "http://tempuri.org/Login";
public final String OPERATION_NAME = "Login";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "http://myserver.com:88/Sample.asmx";
public String Login(String UserName,String Password)
{
Object response=null;
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi;
PropertyInfo pi2;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
pi=new PropertyInfo();
pi.setName("UserName");
pi.setValue(UserName);
pi.setType(PropertyInfo.STRING_CLASS);
request.addProperty(pi);
pi2=new PropertyInfo();
pi2.setName("Password");
pi2.setValue(Password);
pi2.setType(PropertyInfo.STRING_CLASS);
request.addProperty(pi2);
try
{
httpTransport.call(SOAP_ACTION, envelope);
response = envelope.getResponse();
}
catch (Exception exception)
{
response=exception.toString();
}
return response.toString();
}
}
activity_my.xml有1个Buttton,2个EditText和1个TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="@+id/btnLogin"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblResult"
android:text="Result"
android:layout_marginTop="114dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="UserName"
android:id="@+id/txtUserName"
android:layout_toStartOf="@+id/btnLogin"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btnLogin" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:password="true"
android:hint="Password"
android:id="@+id/txtPassword"
android:layout_alignTop="@+id/txtUserNmae"
android:layout_toEndOf="@+id/btnLogin"
android:layout_toRightOf="@+id/btnLogin" />
MyActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button btnLogin= (Button) findViewById(R.id.btnLogin);
btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText txtUserName=(EditText)findViewById(R.id.txtUserName);
EditText txtPassword=(EditText)findViewById(R.id.txtPassword);
TextView lblResult=(TextView)findViewById(R.id.lblResult);
String strUserName=txtUserName.getText().toString();
String strPassword=txtPassword.getText().toString();
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if(TextUtils.isEmpty(strUserName))
{
txtUserName.setError("Enter a UserName");
}
else if(TextUtils.isEmpty(strPassword))
{
txtPassword.setError("Enter a Password");
}
else
{
CallSoap cs=new CallSoap();
try
{
String Result= cs.Login(strUserName, strPassword).toString();
if (Result==strUserName) {
lblResult.setText("Welcome:" + " " + Result);
}
else
{
lblResult.setText("UserName is not found");
txtPassword.setText("");
}
}
catch (Exception e)
{
lblResult.setText(e.toString());
}
}
}
});
}
的AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
我的数据库中有此用户用户:示例密码:12345
当我尝试从Android登录时,我得到:&#34;找不到用户名&#34;。它无法从服务中获得结果。
我的错误在哪里?我没找到。