2

我开发了一款安卓应用。

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

在这里,我调用了 Soap Web 服务。

在这里,我如何验证我的 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!!";
}

 }

这是我的安卓代码:

   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字段。请帮助我

4

2 回答 2

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();}

希望这对你有帮助

于 2012-12-01T08:28:27.107 回答
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();
            }

它成功地为我工作

于 2012-12-01T09:42:12.243 回答