Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. package com.example.chung.myapplication;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. import com.android.volley.RequestQueue;
  11. import com.android.volley.Response;
  12. import com.android.volley.toolbox.Volley;
  13. import com.example.chung.myapplication.Forgot;
  14. import com.example.chung.myapplication.LoginRequest;
  15. import com.example.chung.myapplication.R;
  16.  
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  27.         final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  28.         Button btnLogin = (Button) findViewById(R.id.bLogin);
  29.         Button btnForgot = (Button) findViewById(R.id.bForget);
  30.  
  31.  
  32.         btnForgot.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View v) {
  35.                 Intent intent = new Intent();
  36.                 intent.setClass(MainActivity.this , Forgot.class);
  37.                 MainActivity.this.startActivity(intent);
  38.             }
  39.         });
  40.         btnLogin.setOnClickListener(new View.OnClickListener() {
  41.             String str = etUsername.getEditableText().toString();
  42.             String str2 = etPassword.getEditableText().toString();
  43.             @Override
  44.             public void onClick(View v) {
  45.                 final String username = etUsername.getText().toString();
  46.                 final String password = etPassword.getText().toString();
  47.                 Response.Listener<String> responseListener = new Response.Listener<String>() {
  48.                     @Override
  49.                     public void onResponse(String response) {
  50.                         try {
  51.                             JSONObject jsonResponse = new JSONObject(response);
  52.                             boolean success = jsonResponse.getBoolean("isVaild");
  53.                             String username = jsonResponse.getString("username");
  54.                             String type = jsonResponse.getString("type");
  55.                             String parent = new String("parent");
  56.                             String officer = new String("officer");
  57.                             Intent intent = new Intent(MainActivity.this, Home.class);
  58.                             intent.putExtra("type", type);
  59.                             intent.putExtra("username", username);
  60.                             intent.putExtra("password", password);
  61.                             Intent intent2 = new Intent(MainActivity.this, Home2.class);
  62.                             intent2.putExtra("type", type);
  63.                             intent2.putExtra("username", username);
  64.                             intent2.putExtra("password", password);
  65.                             if (success && type.equals(officer)) {
  66.                                 MainActivity.this.startActivity(intent);
  67.                             }  else if (success && type.equals(parent)){
  68.                                 MainActivity.this.startActivity(intent2);
  69.                             } else {
  70.                                 Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
  71.                             }
  72.  
  73.                         } catch (JSONException e) {
  74.                             e.printStackTrace();
  75.                         }
  76.                     }
  77.                 };
  78.                 LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
  79.                 RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
  80.                 queue.add(loginRequest);
  81.             }
  82.         });
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement