java - 从 Web 服务编辑 Android 中的文本字段验证

标签 java android web-services validation android-edittext

我开发了一款 Android 应用。

应用程序正在执行将值从 android 插入到 mysql 数据库的操作。

这里我调用了 Soap Web 服务。

这里我如何验证我的编辑文本字段。

这是我的网络服务代码:

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){

      }

我如何验证编辑文本字段。请帮助我

最佳答案

您可以通过执行以下操作来验证字符串是否为空: 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();}

希望这对你有帮助

关于java - 从 Web 服务编辑 Android 中的文本字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656384/

相关文章:

java - 通过 HQL 让 child 计数

java - G1 不处理软引用

java - 设计方法(领域驱动或服务驱动)

c# - HTTP Get 请求中的 Unicode 字符

c# - 我需要哪些 SOAP/WS-Security/WS-Addressing/etc 来发送这条消息

java - 为什么我的 swagger 不能在 java 中使用 springboot?

java - Spring security4/login 不通过任何过滤器

java - 当我尝试在 Android Studio 中构建项目时,如何修复此错误?

android - WebView 平滑滚动问题

android - 寻找 : Android Compass API