DATABASE: MYDB
TABLE LOGIN
FIELDS: id, uid, pass
Look Videos Login Registration : https://youtu.be/yU_SC5u5MpU
PHP FILES
connect.php
<?php
$con = mysqli_connect("localhost","root","","mydb");
?>
registration.php
<?php
include "connect.php";
$u=$_GET['unm'];//Query String var
$p=$_GET['pass']; //QueryString Var
$qry="insert into login(uid,pass)values ('$u','$p')";
mysqli_query($con,$qry);
?>
retreg.php
<?php
include "connect.php";
$sql=mysqli_query($con,"select * from login");
while($row=mysqli_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
?>
ANDROID FILES
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
build.gradle (project:LoginApplication)
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
}
build.gradle (Module)
android {
useLibrary 'org.apache.http.legacy'
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN NOW"
android:textAlignment="center"
android:textColor="#E60E80BD"
android:textSize="23sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter UserID"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPersonName" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New User Registration here"
android:textColor="#D3CA0909"
android:textSize="18sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter UserID"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registration Now" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Click Here" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_success.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SuccessActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome Login Success.."
tools:layout_editor_absoluteX="-1dp"
tools:layout_editor_absoluteY="5dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoginActivity.java
package com.example.loginapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity {
EditText uid,pass;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
StrictMode.enableDefaults();
uid = (EditText) findViewById(R.id.editText3);
pass = (EditText) findViewById(R.id.editText4);
login = (Button) findViewById(R.id.button3);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String u = uid.getText().toString().trim();
String p = pass.getText().toString().trim();
String result = null;
InputStream is = null;
StringBuilder sb = null;
if (u.length() == 0) {
Toast.makeText(getApplicationContext(), "Please Enter UID", Toast.LENGTH_SHORT).show();
} else {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/db/retreg.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
//paring data
String u_id;
String u_pass;
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
u_id = json_data.getString("uid").toString().trim();
u_pass = json_data.getString("pass").toString().trim();
// comparing table record with user id and password
if (u.toString().equals(u_id.toString()) && p.toString().equals(u_pass.toString())) {
Intent i1=new Intent(getApplicationContext(),SuccessActivity.class );
startActivity(i1);
}
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No Record Found...", Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
});
}
}
MainActivity.java
package com.example.loginapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class MainActivity extends AppCompatActivity {
EditText uid,pass;
Button login,registration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uid = (EditText) findViewById(R.id.editText);
pass = (EditText) findViewById(R.id.editText2);
login = (Button) findViewById(R.id.button2);
registration = (Button) findViewById(R.id.button);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(i);
}
});
StrictMode.enableDefaults();
registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{ //try
String u= uid.getText().toString();
String p=pass.getText().toString();
HttpClient httpclient = new DefaultHttpClient(); //http service
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/db/registration.php?unm="+u+"&pass="+p);
HttpResponse response = httpclient.execute(httppost);
Toast.makeText(getApplicationContext(), "Registration Success",Toast.LENGTH_LONG).show();
Log.e("pass 1", "connection success ");
} catch (Exception e) { // error then here
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
}
Click to View Login and Registration with android studio with mysql Video : https://youtu.be/yU_SC5u5MpU
No comments:
Post a Comment