Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. package com.example.pc.loginregister;_
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import com.android.volley.Response;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13.  
  14. public class LoginActivity extends AppCompatActivity {
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_login);
  20.  
  21. final EditText etUsername=(EditText) findViewById(R.id.login);
  22. final EditText etPassword=(EditText) findViewById(R.id.password);
  23. final Button bLogin=(Button) findViewById(R.id.loginbutton);
  24.  
  25. bLogin.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. final String login=etUsername.getText().toString();
  29. final String password=etPassword.getText().toString();
  30.  
  31. Response.Listener<String> responseListener=new Response.Listener<String>() {
  32. @Override
  33. public void onResponse(String response) {
  34. try {
  35. JSONObject jsonResponse= new JSONObject(response);
  36. boolean success= jsonResponse.getBoolean("success");
  37.  
  38. if (success)
  39. {
  40. String name=jsonResponse.getString("name");
  41. Intent intent=new Intent(LoginActivity.this, UserAreaActivity.class);
  42. intent.putExtra("name",name);
  43. intent.putExtra("login",login);
  44. intent.putExtra("password",password);
  45. LoginActivity.this.startActivity(intent);
  46. }
  47.  
  48. else {
  49. AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
  50. builder.setMessage("Login Failed").setNegativeButton("Retry",null).create().show();
  51.  
  52. }
  53. } catch (JSONException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. };
  58. LoginRequest loginRequest=new LoginRequest(login,password,responseListener);
  59. }
  60. });
  61. }
  62. }
  63.  
  64. package com.example.pc.loginregister;
  65. import com.android.volley.Response;
  66. import com.android.volley.toolbox.StringRequest;
  67.  
  68. import java.util.HashMap;
  69. import java.util.Map;
  70.  
  71. public class LoginRequest extends StringRequest {
  72. private static final String LOGIN_REQUEST_URL="http://10.0.2.2/attendance/Login.php";
  73. private Map<String, String> params;
  74.  
  75. public LoginRequest(String login, String password, Response.Listener<String> listener)
  76. {
  77. super(Method.POST, LOGIN_REQUEST_URL, listener, null);
  78. params=new HashMap<>();
  79. params.put("login",login);
  80. params.put("password",password);
  81. }
  82.  
  83. @Override
  84. public Map<String, String> getParams() {
  85. return params;
  86. }
  87. }
  88.  
  89. <?xml version="1.0" encoding="utf-8"?>
  90. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  91. xmlns:app="http://schemas.android.com/apk/res-auto"
  92. xmlns:tools="http://schemas.android.com/tools"
  93. android:id="@+id/activity_main"
  94. android:layout_width="match_parent"
  95. android:layout_height="match_parent"
  96. android:paddingTop="16dp"
  97. android:paddingBottom="16dp"
  98. android:paddingLeft="16dp"
  99. android:paddingRight="16dp"
  100. android:orientation="vertical"
  101. android:background="@color/white"
  102. tools:context="com.example.pc.loginregister.LoginActivity">
  103.  
  104. <LinearLayout
  105. android:orientation="vertical"
  106. android:layout_centerHorizontal="true"
  107. android:layout_centerVertical="true"
  108. android:layout_width="match_parent"
  109. android:layout_height="match_parent">
  110.  
  111. <ImageView
  112. android:layout_width="match_parent"
  113. android:layout_height="0dp"
  114. android:layout_weight="1"
  115. android:layout_marginTop="20dp"
  116. android:layout_marginLeft="10dp"
  117. android:layout_marginBottom="10dp"
  118. android:layout_marginRight="10dp"
  119. android:src="@drawable/papl"
  120. android:id="@+id/imageView"/>
  121.  
  122. <EditText
  123. android:id="@+id/login"
  124. android:inputType="textEmailAddress"
  125. android:hint="Email"
  126. style="@style/edittext_style"/>
  127.  
  128. <EditText
  129. android:id="@+id/password"
  130. android:inputType="textPassword"
  131. android:hint="Password"
  132. style="@style/edittext_style"/>
  133.  
  134. <Button
  135. android:id="@+id/loginbutton"
  136. android:text="Login"
  137. style="@style/button_style" />
  138.  
  139. </LinearLayout>
  140.  
  141. </RelativeLayout>
  142.  
  143. <?xml version="1.0" encoding="utf-8"?>
  144. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  145. package="com.example.pc.loginregister">
  146. <uses-permission android:name="android.permssion.INTERNET"/>
  147.  
  148. <application
  149. android:allowBackup="true"
  150. android:icon="@mipmap/ic_launcher"
  151. android:label="@string/app_name"
  152. android:roundIcon="@mipmap/ic_launcher_round"
  153. android:supportsRtl="true"
  154. android:theme="@style/AppTheme">
  155. <activity android:name=".LoginActivity">
  156. <intent-filter>
  157. <action android:name="android.intent.action.MAIN" />
  158.  
  159. <category android:name="android.intent.category.LAUNCHER" />
  160. </intent-filter>
  161. </activity>
  162. <activity android:name=".UserAreaActivity"></activity>
  163. </application>
  164.  
  165. </manifest>
  166.  
  167. <?php
  168. $conn=mysqli_connect("localhost","root","","loginregister");
  169.  
  170. if (!$conn) {
  171. printf("Connect failed: %sn", mysqli_connect_error());
  172. exit();
  173. }
  174.  
  175. $login=$_POST[""];
  176. $password=$_POST[""];
  177.  
  178. $stmt=mysqli_prepare($conn,"SELECT * FROM user WHERE login=? AND password=?");
  179. mysqli_stmt_bind_param($stmt,"ss",$login,$password);
  180. mysqli_stmt_execute($stmt);
  181.  
  182. mysqli_stmt_store_result($stmt);
  183. mysqli_stmt_bind_result($stmt,$emp_id,$name,$login,$password);
  184.  
  185. $response=array();
  186. $response["success"]=false;
  187.  
  188. while(mysqli_stmt_fetch($stmt))
  189. {
  190. $response["success"]=true;
  191. $response["emp_id"]=$emp_id;
  192. $response["name"]=$name;
  193. $response["login"]=$login;
  194. $response["password"]=$password;
  195. }
  196.  
  197. echo json_encode($response);
  198.  
  199. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement