在实现方法登录之前在android中登录表单验证

时间:2016-04-02 10:52:23

标签: android-studio

在尝试调用方法并在mysql中执行查询之前,我需要帮助来过滤登录活动中的空edittext。我是android开发中的新手并且真的迷失了..我遵循了一个在注册和登录方面运行良好的教程但是没有过滤或验证。可悲的是,我没能完全理解如何每个函数/法正在运行的步骤。我会很感激,如果你能给我一个链接到一个更好的页面,一个很好的教程是不是过时或弃用库。我正在使用android 1.5。

我一直在搜索谷歌和线程,但我找不到解决方案,这是可以理解的新手在Android ..

以下是处理登录的Main.java的代码

public class Main extends AppCompatActivity implements View.OnClickListener {

EditText name, password;
String Name, Password;
Context ctx=this;
String NAME=null, PASSWORD=null, EMAIL=null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    name = (EditText) findViewById(R.id.main_name);
    password = (EditText) findViewById(R.id.main_password);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}



public void main_register(View v){
    startActivity(new Intent(this,Register.class));
}
public void main_login(View v){
    Name = name.getText().toString();
    Password = password.getText().toString();
    BackGround b = new BackGround();
    b.execute(Name, Password);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.main_login:

            break;

    }
}

class BackGround extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String name = params[0];
        String password = params[1];
        String data="";
        int tmp;

        try {
            URL url = new URL("http://10.0.2.2/BirdBreedingManagement/scripts/login.php");
            String urlParams = "name="+name+"&password="+password;

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(urlParams.getBytes());
            os.flush();
            os.close();

            InputStream is = httpURLConnection.getInputStream();
            while((tmp=is.read())!=-1){
                data+= (char)tmp;
            }

            is.close();
            httpURLConnection.disconnect();

            return data;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "Exception: "+e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exception: "+e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String s) {
        String err=null;

        try {
            JSONObject root = new JSONObject(s);
            JSONObject user_data = root.getJSONObject("user_data");
            NAME = user_data.getString("name");
            PASSWORD = user_data.getString("password");
            EMAIL = user_data.getString("email");
        } catch (JSONException e) {
            e.printStackTrace();
            err = "Exception: "+e.getMessage();
        }

        Intent i = new Intent(ctx, Home.class);
        i.putExtra("name", NAME);
        i.putExtra("password", PASSWORD);
        i.putExtra("email", EMAIL);
        i.putExtra("err", err);
        startActivity(i);

    }
}

这是php脚本     尝试{

    //$username = "jeel";
    //$pssword = "23456";


    $username = filter_input(INPUT_POST, 'name');
    $pssword =  filter_input(INPUT_POST, 'password');

    if($username == "" ){
        $results = "Invalid Entry";
        echo json_encode(array("user_data"=>$results));
    }else{
        $stmt = $db->prepare('SELECT * '
                        . 'FROM users1 '
                        . 'WHERE name = :uname AND password = :password ');
        $stmt->bindParam(':uname', $username);
        $stmt->bindParam(':password', $pssword);
        $stmt->execute();
        $results = $stmt->fetch(PDO::FETCH_ASSOC);

        if($results > 0 ){
            $response = array();
            echo json_encode(array("user_data"=>$results));
        } else{
            $results = "No Record Found";
            echo json_encode(array("user_data"=>$results));
        }
    }

 }catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();

提前致谢。

2 个答案:

答案 0 :(得分:0)

由于您需要在运行username之前检查passwordAsyncTask字段是否为空,以便从php获取数据。修改main_login(View v)方法,如下所示

 public void main_login(View v){
        Name = name.getText().toString();
        Password = password.getText().toString();
        if(Name==null || Password==null){
         // can alert the user that either one of username or the password is empty by

       Toast.makeText(getActivity(), "Username or the Password is empty",Toast.LENGTH_LONG).show();
        // in here you can restart the current activity
        startActivity(this,Main.class);
        finish();  // to destroy the current activity

        }
        else{
        BackGround b = new BackGround();
        b.execute(Name, Password);
        }
    }

答案 1 :(得分:0)

Kasun Siyambalapitiya的回答让我让代码按预期执行并进行了一些调整..我在这里发布它以分享给其他人也在寻找他们可以尝试的代码,如果Kasun的答案没有符合他们的要求..

`public void main_login(View v){
    Name = name.getText().toString();
    Password = password.getText().toString();

    if(name.getText().length()==0){


        Toast.makeText(getApplicationContext(),
                "Please Enter your name", Toast.LENGTH_LONG).show();

        Intent intent = getIntent();
        startActivity(intent);
    }else{
        Intent intent = getIntent();
        startActivity(intent);
        BackGround b = new BackGround();
        b.execute(Name, Password);
    }
}`

我删除了finish命令以避免应用程序关闭并重新打开。