Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.pc.loginregister;_
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.android.volley.Response;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class LoginActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- final EditText etUsername=(EditText) findViewById(R.id.login);
- final EditText etPassword=(EditText) findViewById(R.id.password);
- final Button bLogin=(Button) findViewById(R.id.loginbutton);
- bLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- final String login=etUsername.getText().toString();
- final String password=etPassword.getText().toString();
- Response.Listener<String> responseListener=new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- try {
- JSONObject jsonResponse= new JSONObject(response);
- boolean success= jsonResponse.getBoolean("success");
- if (success)
- {
- String name=jsonResponse.getString("name");
- Intent intent=new Intent(LoginActivity.this, UserAreaActivity.class);
- intent.putExtra("name",name);
- intent.putExtra("login",login);
- intent.putExtra("password",password);
- LoginActivity.this.startActivity(intent);
- }
- else {
- AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
- builder.setMessage("Login Failed").setNegativeButton("Retry",null).create().show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- };
- LoginRequest loginRequest=new LoginRequest(login,password,responseListener);
- }
- });
- }
- }
- package com.example.pc.loginregister;
- 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="http://10.0.2.2/attendance/Login.php";
- private Map<String, String> params;
- public LoginRequest(String login, String password, Response.Listener<String> listener)
- {
- super(Method.POST, LOGIN_REQUEST_URL, listener, null);
- params=new HashMap<>();
- params.put("login",login);
- params.put("password",password);
- }
- @Override
- public Map<String, String> getParams() {
- return params;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout 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:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingTop="16dp"
- android:paddingBottom="16dp"
- android:paddingLeft="16dp"
- android:paddingRight="16dp"
- android:orientation="vertical"
- android:background="@color/white"
- tools:context="com.example.pc.loginregister.LoginActivity">
- <LinearLayout
- android:orientation="vertical"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:layout_marginTop="20dp"
- android:layout_marginLeft="10dp"
- android:layout_marginBottom="10dp"
- android:layout_marginRight="10dp"
- android:src="@drawable/papl"
- android:id="@+id/imageView"/>
- <EditText
- android:id="@+id/login"
- android:inputType="textEmailAddress"
- android:hint="Email"
- style="@style/edittext_style"/>
- <EditText
- android:id="@+id/password"
- android:inputType="textPassword"
- android:hint="Password"
- style="@style/edittext_style"/>
- <Button
- android:id="@+id/loginbutton"
- android:text="Login"
- style="@style/button_style" />
- </LinearLayout>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.pc.loginregister">
- <uses-permission android:name="android.permssion.INTERNET"/>
- <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">
- <activity android:name=".LoginActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".UserAreaActivity"></activity>
- </application>
- </manifest>
- <?php
- $conn=mysqli_connect("localhost","root","","loginregister");
- if (!$conn) {
- printf("Connect failed: %sn", mysqli_connect_error());
- exit();
- }
- $login=$_POST[""];
- $password=$_POST[""];
- $stmt=mysqli_prepare($conn,"SELECT * FROM user WHERE login=? AND password=?");
- mysqli_stmt_bind_param($stmt,"ss",$login,$password);
- mysqli_stmt_execute($stmt);
- mysqli_stmt_store_result($stmt);
- mysqli_stmt_bind_result($stmt,$emp_id,$name,$login,$password);
- $response=array();
- $response["success"]=false;
- while(mysqli_stmt_fetch($stmt))
- {
- $response["success"]=true;
- $response["emp_id"]=$emp_id;
- $response["name"]=$name;
- $response["login"]=$login;
- $response["password"]=$password;
- }
- echo json_encode($response);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement