Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. 03-31 08:23:33.926 26890-26890/info.androidhive.loginandregistration W/System.err:
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import com.android.volley.Request.Method;
  14. import com.android.volley.Response;
  15. import com.android.volley.VolleyError;
  16. import com.android.volley.toolbox.StringRequest;
  17.  
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import java.util.HashMap;
  22. import java.util.Map;
  23.  
  24. import info.androidhive.loginandregistration.R;
  25. import info.androidhive.loginandregistration.app.AppConfig;
  26. import info.androidhive.loginandregistration.app.AppController;
  27. import info.androidhive.loginandregistration.helper.SQLiteHandler;
  28. import info.androidhive.loginandregistration.helper.SessionManager;
  29.  
  30. public class LoginActivity extends Activity {
  31. private static final String TAG = RegisterActivity.class.getSimpleName();
  32. private Button btnLogin;
  33. private Button btnLinkToRegister;
  34. private EditText inputEmail;
  35. private EditText inputPassword;
  36. private ProgressDialog pDialog;
  37. private SessionManager session;
  38. private SQLiteHandler db;
  39.  
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_login);
  44.  
  45. inputEmail = (EditText) findViewById(R.id.email);
  46. inputPassword = (EditText) findViewById(R.id.password);
  47. btnLogin = (Button) findViewById(R.id.btnLogin);
  48. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  49.  
  50. // Progress dialog
  51. pDialog = new ProgressDialog(this);
  52. pDialog.setCancelable(false);
  53.  
  54. // SQLite database handler
  55. db = new SQLiteHandler(getApplicationContext());
  56.  
  57. // Session manager
  58. session = new SessionManager(getApplicationContext());
  59.  
  60. // Check if user is already logged in or not
  61. if (session.isLoggedIn()) {
  62. // User is already logged in. Take him to main activity
  63. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  64. startActivity(intent);
  65. finish();
  66. }
  67.  
  68. // Login button Click Event
  69. btnLogin.setOnClickListener(new View.OnClickListener() {
  70.  
  71. public void onClick(View view) {
  72. String email = inputEmail.getText().toString().trim();
  73. String password = inputPassword.getText().toString().trim();
  74.  
  75. // Check for empty data in the form
  76. if (!email.isEmpty() && !password.isEmpty()) {
  77. // login user
  78. checkLogin(email, password);
  79. } else {
  80. // Prompt user to enter credentials
  81. Toast.makeText(getApplicationContext(),
  82. "Please enter the credentials!", Toast.LENGTH_LONG)
  83. .show();
  84. }
  85. }
  86.  
  87. });
  88.  
  89. // Link to Register Screen
  90. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  91.  
  92. public void onClick(View view) {
  93. Intent i = new Intent(getApplicationContext(),
  94. RegisterActivity.class);
  95. startActivity(i);
  96. finish();
  97. }
  98. });
  99.  
  100. }
  101.  
  102. /**
  103. * function to verify login details in mysql db
  104. * */
  105. private void checkLogin(final String email, final String password) {
  106. // Tag used to cancel the request
  107. String tag_string_req = "req_login";
  108.  
  109. pDialog.setMessage("Logging in ...");
  110. showDialog();
  111.  
  112. StringRequest strReq = new StringRequest(Method.POST,
  113. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  114.  
  115. @Override
  116. public void onResponse(String response) {
  117. Log.d(TAG, "Login Response: " + response.toString());
  118. hideDialog();
  119.  
  120. try {
  121.  
  122. JSONObject jObj = new JSONObject(response);
  123. boolean error = jObj.getBoolean("error");
  124.  
  125. // Check for error node in json
  126. if (!error) {
  127. // user successfully logged in
  128. // Create login session
  129. session.setLogin(true);
  130.  
  131. // Now store the user in SQLite
  132. String uid = jObj.getString("uid");
  133.  
  134. JSONObject user = jObj.getJSONObject("user");
  135. String name = user.getString("name");
  136. String email = user.getString("email");
  137. String created_at = user
  138. .getString("created_at");
  139.  
  140. // Inserting row in users table
  141. db.addUser(name, email, uid, created_at);
  142.  
  143. // Launch main activity
  144. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  145. startActivity(intent);
  146. finish();
  147. } else {
  148. // Error in login. Get the error message
  149. String errorMsg = jObj.getString("error_msg");
  150. Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
  151. }
  152. } catch (JSONException e) {
  153. // JSON error
  154. e.printStackTrace();
  155. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  156. }
  157.  
  158. }
  159. }, new Response.ErrorListener() {
  160.  
  161. @Override
  162. public void onErrorResponse(VolleyError error) {
  163. Log.e(TAG, "Login Error: " + error.getMessage());
  164. Toast.makeText(getApplicationContext(),
  165. error.getMessage(), Toast.LENGTH_LONG).show();
  166. hideDialog();
  167. }
  168. }) {
  169.  
  170. @Override
  171. protected Map<String, String> getParams() {
  172. // Posting parameters to login url
  173. Map<String, String> params = new HashMap<String, String>();
  174. params.put("email", email);
  175. params.put("password", password);
  176.  
  177. return params;
  178. }
  179.  
  180. };
  181.  
  182. // Adding request to request queue
  183. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  184. }
  185.  
  186. private void showDialog() {
  187. if (!pDialog.isShowing())
  188. pDialog.show();
  189. }
  190.  
  191. private void hideDialog() {
  192. if (pDialog.isShowing())
  193. pDialog.dismiss();
  194. }
  195. }
  196.  
  197. <?php
  198.  
  199. require_once 'include/DB_Functions.php';
  200. $db = new DB_Functions();
  201.  
  202. // json response array
  203. $response = array("error" => FALSE);
  204.  
  205. if (isset($_POST['email']) && isset($_POST['password'])) {
  206.  
  207. // receiving the post params
  208. $email = $_POST['email'];
  209. $password = $_POST['password'];
  210.  
  211. // get the user by email and password
  212. $user = $db->getUserByEmailAndPassword($email, $password);
  213.  
  214. if ($user != false) {
  215. // use is found
  216. $response["error"] = FALSE;
  217. $response["uid"] = $user["unique_id"];
  218. $response["user"]["name"] = $user["name"];
  219. $response["user"]["email"] = $user["email"];
  220. $response["user"]["created_at"] = $user["created_at"];
  221. $response["user"]["updated_at"] = $user["updated_at"];
  222. echo json_encode($response);
  223. } else {
  224. // user is not found with the credentials
  225. $response["error"] = TRUE;
  226. $response["error_msg"] = "Login credentials are wrong. Please try again!";
  227. echo json_encode($response);
  228. }
  229. } else {
  230. // required post params is missing
  231. $response["error"] = TRUE;
  232. $response["error_msg"] = "Required parameters email or password is missing!";
  233. echo json_encode($response);
  234. }
  235. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement