gpt4 book ai didi

java - 从 web 服务编辑 android 中的文本字段验证

转载 作者:行者123 更新时间:2023-11-30 11:38:21 25 4
gpt4 key购买 nike

我开发了一款安卓应用。

应用程序正在执行从 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!!";
}

}

这是我的安卓代码:

   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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com