Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package com.example.kevin.myapplication;
  2.  
  3. import android.app.AlertDialog;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  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.RequestQueue;
  13. import com.android.volley.Response;
  14. import com.android.volley.toolbox.Volley;
  15. import com.example.kevin.myapplication.R;
  16.  
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. public class LoginActivity extends AppCompatActivity {
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_login);
  25.  
  26. final EditText etUsername = (EditText) findViewById(R.id.etUsername);
  27. final EditText etPassword = (EditText) findViewById(R.id.etPassword);
  28. final TextView tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);
  29. final Button bLogin = (Button) findViewById(R.id.bSignIn);
  30.  
  31. tvRegisterLink.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
  35. LoginActivity.this.startActivity(registerIntent);
  36. }
  37. });
  38.  
  39. bLogin.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. final String username = etUsername.getText().toString();
  43. final String password = etPassword.getText().toString();
  44.  
  45. // Response received from the server
  46. Response.Listener<String> responseListener = new Response.Listener<String>() {
  47. @Override
  48. public void onResponse(String response) {
  49. try {
  50. JSONObject jsonResponse = new JSONObject(response);
  51. boolean success = jsonResponse.getBoolean("success");
  52.  
  53. if (success) {
  54. String name = jsonResponse.getString("name");
  55. int age = jsonResponse.getInt("age");
  56.  
  57. Intent intent = new Intent(LoginActivity.this, Menue.class);
  58. intent.putExtra("name", name);
  59. intent.putExtra("age", age);
  60. intent.putExtra("username", username);
  61. LoginActivity.this.startActivity(intent);
  62. } else {
  63. AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
  64. builder.setMessage("Login Failed")
  65. .setNegativeButton("Retry", null)
  66. .create()
  67. .show();
  68. }
  69.  
  70. } catch (JSONException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. };
  75.  
  76. LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
  77. RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
  78. queue.add(loginRequest);
  79. }
  80. });
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement