Android登入註冊
阿新 • • 發佈:2019-01-10
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聯網許可權
<uses-permission android:name="android.permission.INTERNET" />
注:頁面佈局比較簡單就不給出了