0

嘿,我正在将 android 与 mysql 连接,但该应用程序也接受空白登录..所以,没有名称和密码,但仍然登录..使用 eclipse android 2.2 我正在按照本教程工作,这 一切都得到了解释并且有效,但似乎没有人注意到这一点而且我在android编程方面不太好,所以我自己无法解决这个问题

这是我的代码,我在最后添加了 userFunction.loginUser 方法有人帮忙吗?

            package com.app.app;

        import java.util.HashMap;

        import org.json.JSONException;
        import org.json.JSONObject;

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;

        import com.app.app.library.DatabaseHandler;
        import com.app.app.library.UserFunctions;
        import com.app.app.R;

        public class LoginActivity extends Activity {
            Button btnLogin;
            Button btnLinkToRegister;
            EditText inputEmail;
            EditText inputPassword;
            TextView loginErrorMsg;
            // JSON Response node names
            private static String KEY_SUCCESS = "success";
            private static String KEY_ERROR = "error";
            private static String KEY_ERROR_MSG = "error_msg";
            private static String KEY_UID = "uid";
            private static String KEY_NAME = "name";
            private static String KEY_EMAIL = "email";
            private static String KEY_CREATED_AT = "created_at";

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

                // Importing all assets like buttons, text fields
                inputEmail = (EditText) findViewById(R.id.loginEmail);
                inputPassword = (EditText) findViewById(R.id.loginPassword);
                btnLogin = (Button) findViewById(R.id.btnLogin);
                btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
                loginErrorMsg = (TextView) findViewById(R.id.login_error);

                // Login button Click Event
                btnLogin.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {
                        String email = inputEmail.getText().toString();
                        String password = inputPassword.getText().toString();
                        UserFunctions userFunction = new UserFunctions();
                        Log.d("Button", "Login");
                        JSONObject json = userFunction.loginUser(email, password);

                        // check for login response
                        try {
                            if (json.getString(KEY_SUCCESS) != null) {
                                loginErrorMsg.setText("");
                                String res = json.getString(KEY_SUCCESS); 
                                if(Integer.parseInt(res) == 1){
                                    // user successfully logged in
                                    // Store user details in SQLite Database
                                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                                    JSONObject json_user = json.getJSONObject("user");

                                    // Clear all previous data in database
                                    userFunction.logoutUser(getApplicationContext());
                                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                                    // Launch Dashboard Screen
                                    Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                                    // Close all views before launching Dashboard
                                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(dashboard);

                                    // Close Login Screen
                                    finish();
                                }else{
                                    // Error in login
                                    loginErrorMsg.setText("Incorrect username/password");
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                // Link to Register Screen
                btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {
                        Intent i = new Intent(getApplicationContext(),
                                RegisterActivity.class);
                        startActivity(i);
                        finish();
                    }
                });



        and this my sql part of login

在这里

  /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject loginUser(String email, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }

    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject registerUser(String name, String email, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));

        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }

    /**
     * Function get Login status
     * */
    public boolean isUserLoggedIn(Context context){
        DatabaseHandler db = new DatabaseHandler(context);
        int count = db.getRowCount();
        if(count > 0){
            // user logged in
            return true;
        }
        return false;
    }
4

0 回答 0