Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sit.elderlylocate.Register;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import sit.elderlylocate.Login.ActivityLogin;
- import sit.elderlylocate.R;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import android.app.AlertDialog;
- public class ActivityRegister extends Activity {
- EditText username, password, confirmpass, firstname, lastname;
- TextView birthday;
- String Username, Password, Confirmpass, Firstname, Lastname, Birthday;
- Context ctx = this;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- username = (EditText) findViewById(R.id.txtUsername);
- password = (EditText) findViewById(R.id.txtPassword);
- confirmpass = (EditText) findViewById(R.id.txtConfirmPass);
- firstname = (EditText) findViewById(R.id.txtFirstname);
- lastname = (EditText) findViewById(R.id.txtLastname);
- birthday = (TextView) findViewById(R.id.txtBirthday);
- showDialogOnButtonClick();
- // btnClickRegister
- final Button btnRegister = (Button) findViewById(R.id.RegisterButton2);
- // Perform action on click
- btnRegister.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- checkDada();
- RegisterButton2(v);
- Intent i = new Intent(getApplicationContext(), ActivityLogin.class);
- startActivity(i);
- }
- });
- }
- // Get birthday data [choose date]
- Button bDate;
- int year_x,month_x,day_x;
- static final int DIALOG_ID = 0;
- public void showDialogOnButtonClick(){
- bDate = (Button)findViewById(R.id.bDate);
- bDate.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showDialog(DIALOG_ID);
- }
- }
- );
- }
- protected Dialog onCreateDialog(int id){
- if (id==DIALOG_ID){
- return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
- }
- return null;
- }
- public DatePickerDialog.OnDateSetListener dpickerListner =
- new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- year_x = year;
- month_x = monthOfYear+1;
- day_x = dayOfMonth;
- //Toast.makeText(ActivityRegister.this, year_x + "-" + month_x + "-" + day_x,Toast.LENGTH_LONG).show();
- birthday.setText(year_x + "-" + month_x + "-" + day_x);
- }
- };
- public boolean checkDada() {
- // Error Dialog
- final AlertDialog.Builder ad = new AlertDialog.Builder(this);
- ad.setTitle("Error! ");
- ad.setIcon(android.R.drawable.btn_star_big_on);
- ad.setPositiveButton("Close", null);
- // Check Username
- if (username.getText().length() == 0) {
- ad.setMessage("Please input [Username] ");
- ad.show();
- username.requestFocus();
- return false;
- }
- // Check Password
- if (password.getText().length() == 0 || confirmpass.getText().length() == 0) {
- ad.setMessage("Please input [Password/Confirm Password] ");
- ad.show();
- password.requestFocus();
- return false;
- }
- // Check Password and Confirm Password (Match)
- if (!password.getText().toString().equals(confirmpass.getText().toString())) {
- ad.setMessage("Password and Confirm Password Not Match! ");
- ad.show();
- confirmpass.requestFocus();
- return false;
- }
- // Check Name
- if (firstname.getText().length() == 0) {
- ad.setMessage("Please input [Firstname] ");
- ad.show();
- firstname.requestFocus();
- return false;
- }
- // Check lastname
- if (lastname.getText().length() == 0) {
- ad.setMessage("Please input [Lastname] ");
- ad.show();
- lastname.requestFocus();
- return false;
- }
- // Check birthday
- if (birthday.getText().length() == 0) {
- ad.setMessage("Please input [Birthday] ");
- ad.show();
- birthday.requestFocus();
- return false;
- }
- return true;
- }
- public void RegisterButton2(View v) {
- Username = username.getText().toString();
- Password = password.getText().toString();
- Confirmpass = confirmpass.getText().toString();
- Firstname = firstname.getText().toString();
- Lastname = lastname.getText().toString();
- Birthday = year_x + "-" + month_x + "-" + day_x;
- BackGround b = new BackGround();
- b.execute(Username, Password, Confirmpass, Firstname, Lastname, Birthday);
- }
- class BackGround extends AsyncTask<String, String, String> {
- @Override
- protected String doInBackground(String... params) {
- String username = params[0];
- String password = params[1];
- String firstname = params[2];
- String lastname = params[3];
- String birthday = params[4];
- String data = "";
- int tmp;
- try {
- URL url = new URL("http://oldiefinder.esy.es/Android/register.php");
- String urlParams = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname + "&birthday=" + birthday;
- 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) {
- if (s.equals("")) {
- s = "Data saved successfully.";
- }
- Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement