From 07de13c552752302b143e1d568ee4a725d1e5837 Mon Sep 17 00:00:00 2001 From: Priyatham Date: Mon, 16 Mar 2020 19:25:33 +0530 Subject: [PATCH] successful login-reg using web server --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 5 +- .../com/example/alzapp/DatabaseHelper.java | 72 ------------------ .../java/com/example/alzapp/LoginRequest.java | 24 ++++++ .../com/example/alzapp/Registerequest.java | 30 ++++++++ .../com/example/alzapp/UserAreaActivity.java | 32 ++++++++ .../main/java/com/example/alzapp/login.java | 63 ++++++++++++++- .../java/com/example/alzapp/registration.java | 57 ++++++++++---- app/src/main/res/layout/activity_login.xml | 3 + .../main/res/layout/activity_registration.xml | 2 +- .../main/res/layout/activity_user_area.xml | 42 ++++++++++ app/src/main/res/layout/leftarrow3.png | Bin 3782 -> 0 bytes app/src/main/res/values/dimens.xml | 6 ++ 13 files changed, 246 insertions(+), 91 deletions(-) delete mode 100644 app/src/main/java/com/example/alzapp/DatabaseHelper.java create mode 100644 app/src/main/java/com/example/alzapp/LoginRequest.java create mode 100644 app/src/main/java/com/example/alzapp/Registerequest.java create mode 100644 app/src/main/java/com/example/alzapp/UserAreaActivity.java create mode 100644 app/src/main/res/layout/activity_user_area.xml delete mode 100644 app/src/main/res/layout/leftarrow3.png create mode 100644 app/src/main/res/values/dimens.xml diff --git a/app/build.gradle b/app/build.gradle index e1c605c..c67904c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,4 +28,5 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' implementation 'com.android.support:cardview-v7:29.0.2' implementation "androidx.annotation:annotation:1.1.0" + implementation 'com.android.volley:volley:1.1.1' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 97f3d4b..c8d9af5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + - + + diff --git a/app/src/main/java/com/example/alzapp/DatabaseHelper.java b/app/src/main/java/com/example/alzapp/DatabaseHelper.java deleted file mode 100644 index ffb84ba..0000000 --- a/app/src/main/java/com/example/alzapp/DatabaseHelper.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.example.alzapp; - -import androidx.appcompat.app.AppCompatActivity; - -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.widget.Toast; -/******* -Created on: 21/01/2020 - - By: B.Priyatham sai chand - - - ********/ - -public class DatabaseHelper extends SQLiteOpenHelper { - - private static final String DATABASE_NAME = "User.db"; - private static final String TABLE_NAME = "tblUser"; - private static final int DATABASE_VERSION = 2; - private Context context; - - DatabaseHelper(Context context){ - super (context, DATABASE_NAME,null, DATABASE_VERSION); - this.context = context; - - } - - @Override - public void onCreate(SQLiteDatabase db) { - - db.execSQL("CREATE TABLE GENDER (GENDER INTEGER PRIMARY KEY, VALUE VARCHAR(20));"); - db.execSQL("CREATE TABLE TBLUSERINFO (USERID INTEGER PRIMARY KEY AUTOINCREMENT,USERNAME TEXT,PASSWORD TEXT,FIRSTNAME VARCHAR(20),LASTNAME VARCHAR(20), DOB TEXT,EMAIL TEXT, GENDER INTEGER,FOREIGN KEY(GENDER) REFERENCES GENDER(GENDER));"); - db.execSQL("CREATE TABLE TBLUSERNAME(ID INTEGER PRIMARY KEY, USERNAME TEXT,PASSWORD TEXT)"); - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - - db.execSQL("DROP TABLE IF EXISTS TBLUSERINFO;"); - onCreate(db); - } - - public Cursor getAllData() { - SQLiteDatabase db = this.getWritableDatabase(); - Cursor res = db.rawQuery("select * from TBLUSERINFO ORDER BY USERID",null); - return res; - } - - public void insertUsername(String username ,String password){ - SQLiteDatabase db = this.getWritableDatabase(); - db.execSQL("INSERT INTO TBLUSERNAME VALUES (username,password);"); - - } - - - public void insertData(String reg_username,String reg_password,String first_name,String Last_name,String dob, String email,int gender) { - SQLiteDatabase db = this.getWritableDatabase(); - db.execSQL("INSERT INTO TBLUSERINFO VALUES (reg_username,reg_password,first_name,last_name,dob,email,gender);"); - - } - - - public Cursor selectData(String firstname) { - SQLiteDatabase db = this.getWritableDatabase(); - Cursor res = db.rawQuery("select * from TBLUSERINFO WHERE FIRSTNAME='" + firstname + "' ORDER BY ID",null); - return res; - } - -} diff --git a/app/src/main/java/com/example/alzapp/LoginRequest.java b/app/src/main/java/com/example/alzapp/LoginRequest.java new file mode 100644 index 0000000..cc56a8e --- /dev/null +++ b/app/src/main/java/com/example/alzapp/LoginRequest.java @@ -0,0 +1,24 @@ +package com.example.alzapp; + +import com.android.volley.Response; +import com.android.volley.toolbox.StringRequest; + +import java.util.HashMap; +import java.util.Map; + +public class LoginRequest extends StringRequest { + private static final String LOGIN_REQUEST_URL = "https://eclectic-sweeps.000webhostapp.com/login.php"; + private Map params; + + public LoginRequest(String username, String password, Response.Listener listener) { + super(Method.POST, LOGIN_REQUEST_URL, listener, null); + params = new HashMap<>(); + params.put("username", username); + params.put("password", password); + } + + @Override + public Map getParams() { + return params; + } +} diff --git a/app/src/main/java/com/example/alzapp/Registerequest.java b/app/src/main/java/com/example/alzapp/Registerequest.java new file mode 100644 index 0000000..1319932 --- /dev/null +++ b/app/src/main/java/com/example/alzapp/Registerequest.java @@ -0,0 +1,30 @@ +package com.example.alzapp; + +import com.android.volley.Response; +import com.android.volley.toolbox.*; + +import java.util.HashMap; +import java.util.Map; + +public class Registerequest extends StringRequest { + private static final String REGISTER_REQUEST_URL = "https://eclectic-sweeps.000webhostapp.com/registration.php"; + private Map params; + + public Registerequest(String firstname, String lastname,String username,String dob,String email,String password, Response.Listener listener) { + super(Method.POST, REGISTER_REQUEST_URL, listener, null); + params = new HashMap<>(); + params.put("username", username); + params.put("password", password); + params.put("firstname", firstname); + params.put("lastname", lastname); + params.put("dob",dob); + params.put("email",email); + + + } + + @Override + public Map getParams() { + return params; + } +} diff --git a/app/src/main/java/com/example/alzapp/UserAreaActivity.java b/app/src/main/java/com/example/alzapp/UserAreaActivity.java new file mode 100644 index 0000000..02eda22 --- /dev/null +++ b/app/src/main/java/com/example/alzapp/UserAreaActivity.java @@ -0,0 +1,32 @@ +package com.example.alzapp; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; + +import android.content.Intent; + +import android.widget.EditText; +import android.widget.TextView; + +public class UserAreaActivity extends AppCompatActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_user_area); + + Intent intent = getIntent(); + String username = intent.getStringExtra("username"); + + + TextView tvWelcomeMsg = (TextView) findViewById(R.id.tvWelcomeMsg); + EditText etUsername = (EditText) findViewById(R.id.etUsername); + + + // Display user details + String message = username + " welcome to your user area"; + tvWelcomeMsg.setText(message); + etUsername.setText(username); + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/alzapp/login.java b/app/src/main/java/com/example/alzapp/login.java index ff1dbac..79e1308 100644 --- a/app/src/main/java/com/example/alzapp/login.java +++ b/app/src/main/java/com/example/alzapp/login.java @@ -1,13 +1,17 @@ package com.example.alzapp; import androidx.appcompat.app.AppCompatActivity; - +import android.widget.EditText; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; - +import org.json.*; +import java.lang.*; +import com.android.volley.*; +import com.android.volley.toolbox.*; +import android.app.AlertDialog; /******* Created on: 21/01/2020 @@ -18,13 +22,16 @@ import android.widget.TextView; public class login extends AppCompatActivity { private TextView signup; + private Button signin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); - + signin = (Button) findViewById(R.id.signin_button); signup = (TextView) findViewById(R.id.signUp_text); + final EditText etUsername = (EditText) findViewById(R.id.etUsername); + final EditText etPassword = (EditText) findViewById(R.id.etPassword); signup.setOnClickListener(new View.OnClickListener() { @Override @@ -41,6 +48,7 @@ public class login extends AppCompatActivity { + Button back = (Button) findViewById(R.id.back); back.setOnClickListener(new View.OnClickListener() { @@ -50,6 +58,55 @@ public class login extends AppCompatActivity { } }); + signin.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + final String username = etUsername.getText().toString(); + final String password = etPassword.getText().toString(); + + Response.Listener responseListener = new Response.Listener() { + @Override + public void onResponse(String response) { + try { + JSONObject jsonResponse = new JSONObject(response); + boolean success = jsonResponse.getBoolean("success"); + + if (success) { + String username1 = jsonResponse.getString("username"); + + + Intent intent = new Intent(login.this, UserAreaActivity.class); + intent.putExtra("name", username1); + + + login.this.startActivity(intent); + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(login.this); + builder.setMessage("Login Failed") + .setNegativeButton("Retry", null) + .create() + .show(); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + } + }; + + LoginRequest loginRequest = new LoginRequest(username, password, responseListener); + RequestQueue queue = Volley.newRequestQueue(login.this); + queue.add(loginRequest); + + + } + }); + + + + + } diff --git a/app/src/main/java/com/example/alzapp/registration.java b/app/src/main/java/com/example/alzapp/registration.java index a6090d8..3897977 100644 --- a/app/src/main/java/com/example/alzapp/registration.java +++ b/app/src/main/java/com/example/alzapp/registration.java @@ -17,11 +17,16 @@ import java.text.DateFormat; import java.util.Calendar; import android.view.LayoutInflater; import android.widget.ImageView; -import android.widget.TextView; + import android.widget.Toast; -import android.view.View; + import android.view.ViewGroup; import android.view.Gravity; +import org.json.*; +import java.lang.*; +import com.android.volley.*; +import com.android.volley.toolbox.*; +import android.app.AlertDialog; /******* Created on: 21/01/2020 @@ -33,7 +38,7 @@ import android.view.Gravity; public class registration extends AppCompatActivity implements DatePickerDialog.OnDateSetListener { private TextView signin; private TextView dob; - com.example.alzapp.DatabaseHelper dal; + SQLiteDatabase sqLiteDatabase; private RadioGroup radioSexGroup; private RadioButton radioSexButton; @@ -50,8 +55,8 @@ public class registration extends AppCompatActivity implements DatePickerDialog. super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); - dal = new DatabaseHelper(registration.this); - username = (EditText) findViewById(R.id.reg_useername); + + username = (EditText) findViewById(R.id.reg_username); password = (EditText) findViewById(R.id.reg_password); firstname = (EditText) findViewById(R.id.firstname); lastname = (EditText) findViewById(R.id.lastname); @@ -83,9 +88,34 @@ public class registration extends AppCompatActivity implements DatePickerDialog. else { + Response.Listener responseListener = new Response.Listener() { + @Override + public void onResponse(String response) { + try { + JSONObject jsonResponse = new JSONObject(response); + boolean success = jsonResponse.getBoolean("success"); + if (success) { + Intent intent = new Intent(registration.this,login.class); + startActivity(intent); + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(registration.this); + builder.setMessage("Register Failed") + .setNegativeButton("Retry", null) + .create() + .show(); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + }; + Registerequest reg_request = new Registerequest(firstname.getText().toString(),lastname.getText().toString(),username.getText().toString(),dob.getText().toString(),email_id.getText().toString(),password.getText().toString(),responseListener); + RequestQueue queue = Volley.newRequestQueue(registration.this); + queue.add(reg_request); + + - insertData(); } } @@ -151,15 +181,15 @@ public class registration extends AppCompatActivity implements DatePickerDialog. radioSexButton = (RadioButton) findViewById(selectedId); if(radioSexButton.getText() == "Male"){ - sex = 0; + sex = 1; } else if (radioSexButton.getText() == "Female"){ - sex = 1; + sex = 2; } else{ - sex = -1; + sex = 3; } @@ -168,12 +198,7 @@ public class registration extends AppCompatActivity implements DatePickerDialog. } - public void insertData(){ - dal.insertData(username.getText().toString(),lastname.getText().toString(),firstname.getText().toString(),lastname.getText().toString(),dob.getText().toString(),email_id.getText().toString(),sex); - - - } public boolean isEmpty(EditText etText) { return etText.getText().toString().trim().length() == 0; } @@ -196,4 +221,8 @@ public class registration extends AppCompatActivity implements DatePickerDialog. toast.show(); } + + + + } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 9fdca75..e697965 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -46,6 +46,7 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/leftarrow3.png b/app/src/main/res/layout/leftarrow3.png deleted file mode 100644 index 66dacf1664284434e349a1baac636df244150180..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3782 zcmeHJ{ZEru6h5uMpn`xhXbCEvEyz~nLoyOn#9E*)AB92PstZ^G2t#y8$K*S&8%#}% zj20b1F#G_+;WUU}1EFcqIr$LDrihDt4aW%L=sH7!5ZE~lWPgFb^(K9L?|IHW&pFRM z_sx4cUJ>cQbLRno10$Cu09dsEa8>fXy+Fj2flx@BkwE`9nI`LH;470>fq z#ebY`T*aSAD(&6P{Ho{LRb#kso!NM(_|pYsQzVJ!y}KQ6I#SZ?oxvEJPGqe9r+E$y z8{1+`ksa+^7^*6Bg0Y?2J?I2AH_`*Q3ay}8h>!g0G$*L%td{~?umUT$3#y#B(DyAq zo@?#75G0q{LXJpa1({V?BY*g>G~*-D=svwQumaqvPQ#t-y8IK?a3(6y76$CrxPrfX zwl3V3)YCb#az{fsA%;xd+s03=e(Z-zpvo9tB(HoMs435Ndp zccoCDQJRisNXAn#2eJY9X6etj6T-j-%+kPipk{?aOoLAaX4X-t9IUK9oirh| zMvSQEX+kRyWj7EiCR9IW?Fs`tj04w+{RkO1iBa1us>Udy!VXZiwLc?wb}VumuJh68 zel%+{K-g~FWf)wFHf{T*%sAR}8=H@V0><|qWb|>Sf zAx%I%wj$|(&PR)_Fiz=>$T7YRMJg0yBtAu&dgj6i!SFX;3wDI~zoh*$#IJ5jeiDkU zz9;c4Gw#QfUM=c*Z{^^U+c|h_lCfBA1#>iHsj6{#E5I%-ojpY!qJC+~NL-8(((%?! z1^7q~FXFs#0;TomA!8UFtU>-gr35e~b7WHXzwBKDukWqJUQ$xd3d*@Mwi1g%1y8%` zx>_gQY+$O~4ex3sc|_1_w;E395vDSBp{Sz91l?RfbNX^d-h#^DwnGv}Bj~6EIQ0Ah6qwfR9RI z6I59q^c`pTX~3tdlkB_gn!LdR%?QNBzbX^8GEvmG3n==$;Vjzly0PDSQM}*U{T=hE5JG| XI+_H|e)o9+BgDppD`Z!t>aYF-DyPxY diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..f8736ff --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,6 @@ + + + + 16dp + 16dp + \ No newline at end of file