从Web服务编辑android中的文本字段验证

时间:2012-12-01 04:33:52

标签: java android web-services validation android-edittext

我开发了一个Android应用程序。

应用程序执行的值是从android插入到mysql数据库。

我在这里称为Soap网络服务。

这里我如何验证我的edittext字段。

这是我的网络服务代码:

public class RetailerWs {

public String insertData(String Firstname,String Lastname){

 try{

 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xcart-432pro","root","");
 PreparedStatement statement =  con.prepareStatement("INSERT INTO xcart_customers(firstname,lastname) VALUES ('"+Firstname+"','"+Lastname+"');");
 int result = statement.executeUpdate();
  }

 catch(Exception exc){
 System.out.println(exc.getMessage());
  }

return "Registration is  successfull!!";
}

 }

这是我的android代码:

   btninsert = (Button)findViewById(R.id.btn_login);
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         insertValues();
        }
    });
}
 public void insertValues(){
 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
 EditText Firstname = (EditText) findViewById(R.id.tf_userName);
 String user_Name = Firstname.getText().toString();
 EditText Lastname = (EditText) findViewById(R.id.tf_password);
 String user_Password = Lastname.getText().toString();
 PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("Firstname");//Define the variable name in the web service method
    unameProp.setValue(user_Name);//Define value for fname variable
    unameProp.setType(String.class);//Define the type of the variable
    request.addProperty(unameProp);//Pass properties to the variable

  //Pass value for userPassword variable of the web service
    PropertyInfo passwordProp =new PropertyInfo();
    passwordProp.setName("Lastname");
    passwordProp.setValue(user_Password);
    passwordProp.setType(String.class);
    request.addProperty(passwordProp);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try{
     androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


        TextView result = (TextView) findViewById(R.id.result);
        result.setText(response.toString());

       }
     catch(Exception e){

      }

如何验证edittext字段。请帮助我

2 个答案:

答案 0 :(得分:1)

您可以通过执行以下操作验证字符串是否为空: 1-首先在您的类中声明您的EditText字段,而不是在您的方法中:

EditText Firstname;
EditText Lastname;

2-然后在onCreate()方法中初始化这些变量:

Firstname = (EditText) findViewById(R.id.tf_userName);
Lastname = (EditText) findViewById(R.id.tf_password);

3-调用insertValues()之前;方法执行以下操作:

if(Firstname.equals(null)||Lastname.equals(null))
{Toast.makeText(context, "Enter Firstname and/or LastName", Toast.LENGTH_SHORT).show();}
else
{insertValues();}

希望这能帮到你

答案 1 :(得分:0)

你必须在这个Android代码中使用以下条件:

            EditText Firstname = (EditText) findViewById(R.id.tf_userName);
            EditText Lastname = (EditText) findViewById(R.id.tf_password);
            if(Firstname.getText().length()==0)
            {
                Firstname.setError("please enter the firstname");
            }
            else if(Lastname.getText().length()==0)
                {
                    Lastname.setError("please enter the lastname");
            }
            else
            {   
                insertValues();
            }

它成功地为我工作