Android 登录注册

学了一段时间的Android开发,通过写博客记录一下学习过程遇到的问题和一些想要分享的东西。
页面效果如图所示:

整个登录和注册页面都是连接服务器进行操作的,客户端向服务端发出请求,服务端接收请求并进行相应的处理。登录过程中,客户端将用户输入的账号和密码发送给服务器,服务端查询数据库验证登录是否成功,返回登录结果。注册过程中,服务端先查询用户输入的账号是否存在,若存在则要求用户更改账号名,不存在则对数据库执行插入数据操作,返回注册结果。

1、登录页面代码

public class LoginActivity extends Activity {    private SharedPreferences.Editor editor;    private EditText accountEdit;    private EditText passwordEdit;    private Button login;    private static final int OK = 200;    private CheckBox rememberPass;    private SharedPreferences sp;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        //getWindow().setBackgroundDrawableResource(R.drawable.login);        sp = getSharedPreferences("info1.txt", MODE_PRIVATE);        accountEdit = (EditText) findViewById(R.id.user);        passwordEdit = (EditText) findViewById(R.id.pass);        rememberPass = (CheckBox) findViewById(R.id.remember_pass);        login = (Button) findViewById(R.id.login);        boolean isRemember = sp.getBoolean("remember_password", false);        if (isRemember) {            //将账号密码都设置到文本框中            String account = sp.getString("account", "");            String password = sp.getString("password", "");            accountEdit.setText(account);            passwordEdit.setText(password);            rememberPass.setChecked(true);        }        login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                final String username = accountEdit.getText().toString();                final String password = passwordEdit.getText().toString();                //服务端路径                final String serverPath = "http://xxx.xxx.xxx.xxx:8080/ServletTest/login";                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {                    Toast.makeText(LoginActivity.this,"用户名或密码不能为空!",Toast.LENGTH_SHORT).show();                } else {                    editor = sp.edit();                    if (rememberPass.isChecked()) {                        editor.putBoolean("remember_password", true);                        editor.putString("account", username);                        editor.putString("password", password);                    } else {                        editor.clear();                    }                    editor.commit();                    new Thread(new Runnable() {                        @Override                        public void run() {                            try {                                //使用GET方式请求服务器只能这样                                URL url = new URL(serverPath + "?username=" + username + "&password=" + password);                                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();                                httpURLConnection.setRequestMethod("GET");                                httpURLConnection.setConnectTimeout(5000);                                httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");                                int responseCode = httpURLConnection.getResponseCode();                                if (200 == responseCode) {                                    InputStream inputStream = httpURLConnection.getInputStream();                                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));                                    final String responseMsg = bufferedReader.readLine();                                    runOnUiThread(new Runnable() {                                        @Override                                        public void run() {                                            if (responseMsg.equals("true")){                                                Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_LONG).show();                                            }else {                                                Toast.makeText(LoginActivity.this, "登录失败!", Toast.LENGTH_LONG).show();                                            }                                        }                                    });                                    bufferedReader.close();                                    httpURLConnection.disconnect();                                } else {                                                               }                            } catch (Exception e) {                                e.printStackTrace();                            }                        }                    }).start();                }            }        });        TextView tv_register=(TextView) findViewById(R.id.register);        tv_register.setClickable(true);        tv_register.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {            //点击“没有账号?去注册”跳转至注册页面                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);                startActivity(intent2);            }        });    }}

2、注册页面代码

public class RegisterActivity extends Activity implements View.OnClickListener {    private EditText user;    private EditText pw;    private EditText pw2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_register);        user = (EditText) findViewById(R.id.et_user_register);        pw = (EditText) findViewById(R.id.et_pass_register);        pw2 = (EditText) findViewById(R.id.et_pass_register2);        Button button = (Button) findViewById(R.id.register);        button.setOnClickListener(this);    }    @Override    public void onClick(View v) {        final String username1 = user.getText().toString().trim();        final String password1 = pw.getText().toString().trim();        final String password2 = pw2.getText().toString().trim();        //服务端地址        final String serverPath = "http://xxx.xxx.xxx.xxx:8080/ServletTest/register";        if (TextUtils.isEmpty(username1) || TextUtils.isEmpty(password1) || TextUtils.isEmpty(password2)) {            Toast.makeText(RegisterActivity.this, "请输入用户名,密码和确认密码!!!", Toast.LENGTH_SHORT).show();        } else {            if (!(password1.equals(password2))) {                Toast.makeText(RegisterActivity.this, "两次输入的密码不同,请重新输入!!!", Toast.LENGTH_SHORT).show();            } else {                new Thread(new Runnable() {                    @Override                    public void run() {                        try {                            //使用GET方式请求服务器只能这样                            URL url = new URL(serverPath + "?username=" + username1 + "&password=" + password1);                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();                            httpURLConnection.setRequestMethod("GET");                            httpURLConnection.setConnectTimeout(5000);                            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");                            final int responseCode = httpURLConnection.getResponseCode();                            if (200 == responseCode) {                                InputStream inputStream = httpURLConnection.getInputStream();                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));                                final String responseMsg = bufferedReader.readLine();                                runOnUiThread(new Runnable() {                                    @Override                                    public void run() {                                        if (responseMsg.equals("equal")){                                            Toast.makeText(RegisterActivity.this, "账号存在,请更改用户名!!", Toast.LENGTH_LONG).show();                                        }else if(responseMsg.equals("true")){                                            Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_LONG).show();                                            //注册成功跳转登录界面                                            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);                                            startActivity(intent);                                        }else {                                            Toast.makeText(RegisterActivity.this, "注册失败!", Toast.LENGTH_LONG).show();                                        }                                    }                                });                                bufferedReader.close();                                httpURLConnection.disconnect();                            } else {                                                            }                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                }).start();            }        }    }}

3、在AndroidManifest.xml清单文件中添加Android联网权限

注:页面布局比较简单就不给出了

更多相关文章

  1. Eclipse如何打包生成Apk安装包实例(图)
  2. Android忘记密码功能实现
  3. android studio生成签名导打包的方法
  4. Android(安卓)发送Post请求上传图片(以InputStream为数据交互)
  5. 关于Android中aidl的关键词in,out,inout的探索
  6. 谨慎使用第三方软件!安卓新型恶意软件,可窃取用户银行密码与电子钱
  7. 打包状态下百度的ak的获得
  8. Android(安卓)ListView分页加载(服务端+android端)Demo
  9. SignalR在Android上的实践

随机推荐

  1. android 一张图片实现 ImageView 实现 点
  2. dpi、dp、sp、px、mm之间的关系
  3. Android重力感应示例 .
  4. android 蓝牙模块的串口通信
  5. Android内存小谈 - Birdmafly
  6. Android中常见的坑有哪些?
  7. 作为一个移动应用开发者,我们的创意应当避
  8. Android实现View滑动的6种方式
  9. 知乎 Android(安卓)客户端三方库敏感代码
  10. Android(安卓)短信广播接收相关问题