首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法连接数据库

无法连接数据库
EN

Stack Overflow用户
提问于 2012-11-27 13:57:03
回答 1查看 147关注 0票数 0

我正在尝试使用eclipse创建一个应用程序。在用户注册页面中,我输入了所有需要的数据,然后单击提交按钮,出现消息“不幸的是,您的eclipse已停止”。这条消息意味着什么,如何解决它?

代码语言:javascript
复制
 public class UserRegister extends Activity {

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
RadioButton button1;
RadioButton button2;
Button button3;
int success = 0;

// url to create new product
private static String url_register_user = "http://192.168.1.100/MEMS/add_user.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_register);

    // Edit Text
    inputName = (EditText) findViewById(R.id.nameTextBox);
    inputUsername = (EditText) findViewById(R.id.usernameTextBox);
    inputEmail = (EditText) findViewById(R.id.emailTextBox);
    inputPassword = (EditText) findViewById(R.id.pwTextBox);

    // Create button
    //RadioButton button1 = (RadioButton) findViewById(R.id.studButton);
    // RadioButton button2 = (RadioButton) findViewById(R.id.shopownerButton);
    Button button3 = (Button) findViewById(R.id.regSubmitButton);

    // button click event
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String name = inputName.getText().toString();
            String username = inputUsername.getText().toString();
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();

                   if(name.contentEquals("")||username.contentEquals("")||email.contentEquals("")||password.contentEquals(""))
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this);

                // 2. Chain together various setter methods to set the dialog characteristics
                builder.setMessage(R.string.nullAlert)
                   .setTitle(R.string.alertTitle);

                builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User clicked OK button
                   }
               });

            // 3. Get the AlertDialog from create()
            AlertDialog dialog = builder.show();
            }
            else
            {
                new RegisterNewUser().execute();
            }

        }
    });
}


       class RegisterNewUser extends AsyncTask<String, String, String>{
   protected String doInBackground(String... args) {
       String name = inputName.getText().toString();
       String username = inputUsername.getText().toString();
       String email = inputEmail.getText().toString();
       String password = inputPassword.getText().toString();

       // Building Parameters
       List<NameValuePair> params = new ArrayList<NameValuePair>();
       params.add(new BasicNameValuePair("name", name));
       params.add(new BasicNameValuePair("username", username));
       params.add(new BasicNameValuePair("email", email));
       params.add(new BasicNameValuePair("password", password));

       // getting JSON Object
       // Note that create product url accepts POST method
       JSONObject json = jsonParser.makeHttpRequest(url_register_user,
                           "GET", params);
                   // check log cat for response
       Log.d("Send Notification", json.toString());

       try
           {
                   int success = json.getInt(TAG_SUCCESS);

                   if (success == 1)
                   {
                       // successfully created product
                    Intent i = new Intent(getApplicationContext(), StudentLogin.class);
                    startActivity(i);
                    finish();
                   }

                   else
                   {
                       // failed to register

                   }
           }

            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return null;
           }
                        }
                          }

我的php文件:

代码语言:javascript
复制
<?php

  /*
   * Following code will create a new product row
   * All product details are read from HTTP Post Request
   */

  // array for JSON response
  $response = array();
   // include db connect class
   require_once __DIR__ . '/db_connect.php';

// connecting to db
     $db = new DB_CONNECT();

     // check for required fields
     if (isset($_GET['name']) && isset($_GET['username']) && isset($_GET['email']) &&             isset($_GET['password'])) {

$name = $_GET['name'];
$username = $_GET['username'];
$email = $_GET['email'];
$password = $_GET['password'];


// mysql inserting a new row
$result = mysql_query("INSERT INTO register(name, username, email, password) VALUES('$name', '$username', '$email', '$password')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "You are successfully registered to MEMS.";

    // echoing JSON response
    echo json_encode($response);
}
else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
      // required field is missing
      $response["success"] = 0;
      $response["message"] = "Required field(s) is missing";

        // echoing JSON response
        echo json_encode($response);
        }
           ?>
EN

回答 1

Stack Overflow用户

发布于 2012-11-27 14:07:31

在代码中将比较方法更改为equals()而不是contentEquals()

因为String#equals()不仅比较字符串的内容,而且还检查另一个对象是否也是String的实例。String#contentEquals() methods仅比较内容(字符序列),而不检查另一个对象是否也是String的实例。它可以是任何东西,只要它是CharSequence的实现或StringBuffer的实例。

所以像这样修改你的代码

代码语言:javascript
复制
if(name.equals("")||username.equals("")||email.equals("")||password.equals(""))
 {
     ....
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13578189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档