使用KSOAP2从eclipse调用web方法

时间:2012-07-01 10:03:06

标签: android eclipse web-services ksoap2

我使用visual studio创建了一个Web服务并对其进行了测试,它运行正常。我试图从eclipse调用web方法。我没有得到任何错误,但问题是没有任何反应。它应该将从用户获取的值插入数据库。我已经给出了下面的java类和web方法。我究竟做错了什么?请帮忙

////用于调用传递值/////

的Web方法的Java类
public class Registration extends Activity{
private static final String SOAP_ACTION = "http://tempuri.org/register";
private static final String OPERATION_NAME = "register";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx";

Button sqlRegister;
EditText  sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.registration);
sqlFirstName = (EditText) findViewById(R.id.etFname);
sqlLastName = (EditText) findViewById(R.id.etLname);
sqlEmail = (EditText) findViewById(R.id.etEmail);
sqlMobileNumber = (EditText) findViewById(R.id.etPhone);
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc);

sqlUsername = (EditText) findViewById(R.id.etUsername);
sqlPassword = (EditText) findViewById(R.id.etPwd);

sqlRegister = (Button) findViewById(R.id.bRegister);
sqlRegister.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        switch (v.getId()){
        case R.id.bRegister:
             try{
                String firstname = sqlFirstName.getText().toString();
                String lastname = sqlLastName.getText().toString();
                String emailadd = sqlEmail.getText().toString();
                String number = sqlMobileNumber.getText().toString();
                String loc = sqlCurrentLocation.getText().toString();
                String uname = sqlUsername.getText().toString();
                String pwd = sqlPassword.getText().toString();

                SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

                PropertyInfo pi = new PropertyInfo();
                pi.setName("fname");
                pi.setValue(firstname);
                pi.setType(int.class);
                Request.addProperty(pi);

                PropertyInfo pi2 = new PropertyInfo();
                pi2.setName("lname");
                pi2.setValue(lastname);
                pi2.setType(int.class);
                Request.addProperty(pi2);

                PropertyInfo pi3 = new PropertyInfo();
                pi2.setName("email");
                pi2.setValue(emailadd);
                pi2.setType(int.class);
                Request.addProperty(pi3);

                PropertyInfo pi4 = new PropertyInfo();
                pi2.setName("num");
                pi2.setValue(number);
                pi2.setType(int.class);
                Request.addProperty(pi4);

                PropertyInfo pi5 = new PropertyInfo();
                pi2.setName("locID");
                pi2.setValue(loc);
                pi2.setType(int.class);
                Request.addProperty(pi5);

                PropertyInfo pi6 = new PropertyInfo();
                pi2.setName("username");
                pi2.setValue(uname);
                pi2.setType(int.class);
                Request.addProperty(pi6);

                PropertyInfo pi7 = new PropertyInfo();
                pi2.setName("password");
                pi2.setValue(pwd);
                pi2.setType(int.class);
                Request.addProperty(pi7);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(Request);
                AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(SOAP_ADDRESS);
                try
                {
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapObject response = (SoapObject)envelope.getResponse();
                    int result =  Integer.parseInt(response.getProperty(0).toString());
                    if(result ==1){
                        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show();
                    }
                }
                catch(Exception e)
                {
                   e.printStackTrace();
                }
            break;
        }
      }
    });
}

}

/// Visual Studio中的Web方法////

    [WebMethod]
    public int register(string fname, string lname, string email, string num, int locID,string username,string password)
{
    SqlConnection conn;
    conn = ConnectionManager.GetConnection();
    conn.Open();
    string cmdregister = "INSERT into [User] values(@fname,@lname,@email,@num,@locID)";
    SqlCommand sqlCommand = new SqlCommand(cmdregister, conn);
    sqlCommand.CommandType = CommandType.Text;

    sqlCommand.Parameters.Add("@fname", SqlDbType.Text).Value = fname;
    sqlCommand.Parameters.Add("@lname", SqlDbType.Text).Value = lname;
    sqlCommand.Parameters.Add("@email", SqlDbType.Text).Value = email;
    sqlCommand.Parameters.Add("@num", SqlDbType.Text).Value = num;
    sqlCommand.Parameters.Add("@locID", SqlDbType.Int).Value = locID;
    sqlCommand.ExecuteNonQuery();

    string cmdlogin = "INSERT into [Login] values(@username,@id,@password)";
    SqlCommand sqllogin = new SqlCommand(cmdlogin, conn);
    sqllogin.CommandType = CommandType.Text;

    sqllogin.Parameters.Add("@username", SqlDbType.Text).Value = username;
    sqllogin.Parameters.Add("@password", SqlDbType.Text).Value = password;
    sqllogin.ExecuteNonQuery();
    conn.Close();
    return 1;
}

1 个答案:

答案 0 :(得分:0)

感谢某人,我发现了什么问题。

1)我用过:

AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(SOAP_ADDRESS); 

相反它应该是:

HttpTransportSE httptransport = new HttpTransportSE(SOAP_ADDRESS);

2)我用过:

            PropertyInfo pi = new PropertyInfo();
            pi.setName("fname");
            pi.setValue(firstname);
            pi.setType(int.class);
            Request.addProperty(pi);

相反它应该是:

request.addProperty("fname", String.valueOf(firstname));

对于其他文本字段