Guest User

Untitled

a guest
Dec 6th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. package ph.com.directagent5.daps;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. import com.android.volley.Request;
  13. import com.android.volley.RequestQueue;
  14. import com.android.volley.Response;
  15. import com.android.volley.toolbox.Volley;
  16.  
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. public class LoginActivity extends AppCompatActivity {
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_login);
  26.  
  27. final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  28. final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  29. final Button bLogin = (Button) findViewById(R.id.bLogin);
  30. final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegister);
  31. final TextView tvForgotPasswordLink = (TextView) findViewById(R.id.tvForgotPassword);
  32.  
  33. // When user clicks Register link
  34. tvRegisterLink.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. Intent tvRegisterIntent = new Intent(LoginActivity.this, RegisterActivity.class);
  38. LoginActivity.this.startActivity(tvRegisterIntent);
  39. }
  40. });
  41.  
  42. // When user clicks the login button
  43. bLogin.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46.  
  47. final String username = etUsername.getText().toString();
  48. final String password = etPassword.getText().toString();
  49.  
  50. Response.Listener<String> responseListener = new Response.Listener<String>() {
  51. @Override
  52. public void onResponse(String response) {
  53.  
  54. try {
  55. JSONObject jsonResponse = new JSONObject(response);
  56. boolean success = jsonResponse.getBoolean("success");
  57.  
  58. if(success){
  59.  
  60. String branch = jsonResponse.getString("branch");
  61.  
  62. Intent intent = new Intent(LoginActivity.this, UserActivity.class);
  63. intent.putExtra("branch", branch);
  64.  
  65. LoginActivity.this.startActivity(intent);
  66.  
  67. } else {
  68. AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
  69. builder.setMessage("Login Failed")
  70. .setNegativeButton("Retry", null)
  71. .create()
  72. .show();
  73. }
  74. } catch (JSONException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. };
  79.  
  80. LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
  81. RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
  82. queue.add(loginRequest);
  83. }
  84. });
  85. }
  86. }
  87.  
  88. package ph.com.directagent5.daps;
  89.  
  90.  
  91. import com.android.volley.Request;
  92. import com.android.volley.Response;
  93. import com.android.volley.toolbox.StringRequest;
  94.  
  95. import java.util.HashMap;
  96. import java.util.Map;
  97.  
  98.  
  99. public class LoginRequest extends StringRequest {
  100.  
  101. private static final String LOGIN_REQUEST_URL = "https://example.com/beta/index.php/api/userLogin";
  102. private Map<String, String> params;
  103.  
  104. public LoginRequest(String username, String password, Response.Listener<String> listener) {
  105. super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
  106. params = new HashMap<>();
  107. params.put("username", username);
  108. params.put("password", password);
  109. }
  110.  
  111. @Override
  112. public Map<String, String> getParams() {
  113. return params;
  114. }
  115. }
  116.  
  117. function userLogin_post() {
  118.  
  119. $username = $this->post('username');
  120. $password = $this->post('password');
  121.  
  122. // $username = $_POST['username'];
  123. // $password = $_POST['password'];
  124.  
  125. // $username = '0001';
  126. // $password = '2125';
  127.  
  128. // Check in database
  129. $result = $this->user->login_user($username, $password);
  130.  
  131. $response = array();
  132. $response['success'] = false;
  133. if($result){
  134. foreach ($result as $row) {
  135. $response['id'] = $row->id;
  136. $response['branch'] = $row->members_branch;
  137. $response['name'] = $row->members_name;
  138. $response['email'] = $row->members_email;
  139. $response['username'] = $row->members_username;
  140. $response['success'] = true;
  141. }
  142. }
  143.  
  144. $result = json_encode($response);
  145. $this->response($result, REST_Controller::HTTP_CREATED);
  146. }
Add Comment
Please, Sign In to add comment