Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ph.com.directagent5.daps;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.toolbox.Volley;
- 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.etUsername);
- final EditText etPassword = (EditText) findViewById(R.id.etPassword);
- final Button bLogin = (Button) findViewById(R.id.bLogin);
- final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegister);
- final TextView tvForgotPasswordLink = (TextView) findViewById(R.id.tvForgotPassword);
- // When user clicks Register link
- tvRegisterLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent tvRegisterIntent = new Intent(LoginActivity.this, RegisterActivity.class);
- LoginActivity.this.startActivity(tvRegisterIntent);
- }
- });
- // When user clicks the login button
- bLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- final String username = 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 branch = jsonResponse.getString("branch");
- Intent intent = new Intent(LoginActivity.this, UserActivity.class);
- intent.putExtra("branch", branch);
- 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(username, password, responseListener);
- RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
- queue.add(loginRequest);
- }
- });
- }
- }
- package ph.com.directagent5.daps;
- import com.android.volley.Request;
- 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 = "https://example.com/beta/index.php/api/userLogin";
- private Map<String, String> params;
- public LoginRequest(String username, String password, Response.Listener<String> listener) {
- super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
- params = new HashMap<>();
- params.put("username", username);
- params.put("password", password);
- }
- @Override
- public Map<String, String> getParams() {
- return params;
- }
- }
- function userLogin_post() {
- $username = $this->post('username');
- $password = $this->post('password');
- // $username = $_POST['username'];
- // $password = $_POST['password'];
- // $username = '0001';
- // $password = '2125';
- // Check in database
- $result = $this->user->login_user($username, $password);
- $response = array();
- $response['success'] = false;
- if($result){
- foreach ($result as $row) {
- $response['id'] = $row->id;
- $response['branch'] = $row->members_branch;
- $response['name'] = $row->members_name;
- $response['email'] = $row->members_email;
- $response['username'] = $row->members_username;
- $response['success'] = true;
- }
- }
- $result = json_encode($response);
- $this->response($result, REST_Controller::HTTP_CREATED);
- }
Add Comment
Please, Sign In to add comment