Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中mysql上也插入了空值_Android_Mysql_Web Services_Ksoap2 - Fatal编程技术网

android中mysql上也插入了空值

android中mysql上也插入了空值,android,mysql,web-services,ksoap2,Android,Mysql,Web Services,Ksoap2,您好,我已经开发了将值插入mysql数据库。单击按钮后我没有输入详细信息,这意味着也插入了空值。因此,请帮助我。如何在此处验证。 dis是我的代码: public class RegisterActivity extends Activity{ private final String NAMESPACE = "http://xcart.com"; private final String URL = "http://192.168.1.168:8085/XcartLogin/service

您好,我已经开发了将值插入mysql数据库。单击按钮后我没有输入详细信息,这意味着也插入了空值。因此,请帮助我。如何在此处验证。 dis是我的代码:

  public class RegisterActivity extends Activity{
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8085/XcartLogin/services/Insert?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
private Button btn_cancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    btninsert = (Button)findViewById(R.id.btn_insert);

    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         insertValues();
         EditText userName = (EditText) findViewById(R.id.editText1);
         if( userName.getText().toString().trim().equals(""))
         {    
           userName.setError( "username is required!" );
           //You can Toast a message here that the Username is Empty
         }
         EditText userPassword = (EditText) findViewById(R.id.editText2);
         if( userPassword.getText().toString().trim().equals(""))
         {    
             userPassword.setError( "password is required!" );
           //You can Toast a message here that the Username is Empty
         }
        else
        {
           Intent i = new Intent(getApplicationContext(), AndroidLoginExampleActivity.class);
           startActivity(i);
        }


             }
         });




}
public void insertValues(){
 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
 EditText userName = (EditText) findViewById(R.id.editText1);
 String user_Name = userName.getText().toString();



 EditText userPassword = (EditText) findViewById(R.id.editText2);
 String user_Password = userPassword.getText().toString();


 //Pass value for userName variable of the web service
    PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("userName");//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("userPassword");
    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.textView2);
        result.setText(response.toString());

 }
 catch(Exception e){

 }

}

 }.
Web服务代码为:

package com.xcart;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Insert {

public String insertData(String userName,String userPassword){

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(login,password) VALUES ('"+userName+"','"+userPassword+"');");
  int result = statement.executeUpdate();
  }

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

  return "Insertion successfull!!";
  }

  }

请帮助我。

定义此
EditText用户名=(EditText)findViewById(R.id.editText1)仅在oncreate中使用一次,并在整个活动中使用它们。在您的代码中,您正在调用insertValues()方法,并且您正在插入值,而不检查此方法中的空字符串。这就是原因

您的代码应该如下所示

public class RegisterActivity extends Activity{

 EditText userName, userPassword ;  // << changes here
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8085/XcartLogin/services/Insert?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
private Button btn_cancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    btninsert = (Button)findViewById(R.id.btn_insert);
  user name = (EditText) findViewById(R.id.editText1);// << changes here
userPassword = (EditText) findViewById(R.id.editText2);// << changes here

    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


         if( userName.getText().toString().trim().equals(""))
         {    
           userName.setError( "username is required!" );
           //You can Toast a message here that the Username is Empty
         }
         EditText 
         if( userPassword.getText().toString().trim().equals(""))
         {    
             userPassword.setError( "password is required!" );
           //You can Toast a message here that the Username is Empty
         }
        else
        {   insertValues();  // << changes here
           Intent i = new Intent(getApplicationContext(), AndroidLoginExampleActivity.class);
           startActivity(i);
        }


             }
         });
}
公共类注册表活动扩展活动{
编辑文本用户名、用户密码//