Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. public class Login extends AppCompatActivity implements View.OnClickListener {
  2. Button bLogin;
  3. TextView registerLink;
  4. EditText etUsername, etPassword;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_login);
  10.  
  11. bLogin = (Button) findViewById(R.id.bLogin);
  12. etUsername = (EditText) findViewById(R.id.etUsername);
  13. etPassword = (EditText) findViewById(R.id.etPassword);
  14. registerLink = (TextView) findViewById(R.id.tvRegisterLink);
  15.  
  16.  
  17. bLogin.setOnClickListener(this);
  18. registerLink.setOnClickListener(this);
  19.  
  20.  
  21.  
  22. }
  23.  
  24. @Override
  25. public void onClick(View view) {
  26. switch (view.getId()) {
  27. case R.id.bLogin:
  28. String Username = etUsername.getText().toString();
  29. String Password = etPassword.getText().toString();
  30. Toast.makeText(this, "Loginin...", Toast.LENGTH_SHORT).show();
  31. new Authenticate(this).execute(Username, Password);
  32.  
  33.  
  34.  
  35.  
  36.  
  37. break;
  38. case R.id.tvRegisterLink:
  39. Intent registerIntent = new Intent(Login.this, Register.class);
  40. startActivity(registerIntent);
  41. break;
  42. }
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. public void ParentLogin()
  51. {
  52.  
  53. String Username = etUsername.getText().toString();
  54. String Password = etPassword.getText().toString();
  55. Intent Main = new Intent(Login.this, MainActivity.class);
  56. Main.putExtra("class","Parent");
  57. Main.putExtra("username",Username);
  58. Main.putExtra("password",Password);
  59. startActivity(Main);
  60.  
  61. }
  62. public void NannyLogin()
  63. {
  64.  
  65. String Username = etUsername.getText().toString();
  66. String Password = etPassword.getText().toString();
  67. Intent Main = new Intent(Login.this, MainActivity.class);
  68. Main.putExtra("class", "Nanny");
  69. Main.putExtra("username",Username);
  70. Main.putExtra("password",Password);
  71. startActivity(Main);
  72.  
  73. }
  74.  
  75. public class Authenticate extends AsyncTask<String, Void, String> {
  76.  
  77. private Context context;
  78.  
  79. public Authenticate(Context context) {
  80. this.context = context;
  81. }
  82.  
  83. @Override
  84. protected void onPreExecute() {
  85.  
  86. }
  87. @Override
  88. protected String doInBackground(String... arg0) {
  89. String Username = arg0[0];
  90. String Password = arg0[1];
  91.  
  92. String link;
  93. String data;
  94. BufferedReader bufferedReader;
  95. String result;
  96.  
  97. try {
  98.  
  99. data = "?username=" + URLEncoder.encode(Username, "UTF-8");
  100. data += "&password=" + URLEncoder.encode(Password, "UTF-8");
  101.  
  102.  
  103. link = "http://juliusgoh.comxa.com/Authenticate.php" + data;
  104. URL url = new URL(link);
  105. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  106.  
  107. bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  108. result = bufferedReader.readLine();
  109. return result;
  110. } catch (Exception e) {
  111. return ("Exception: " + e.getMessage());
  112. }
  113.  
  114. }
  115.  
  116. @Override
  117. protected void onPostExecute(String result) {
  118. if (result != null) {
  119. try {
  120. JSONObject jsonObj = new JSONObject(result);
  121. String query_result = jsonObj.getString("query_result");
  122. switch (query_result) {
  123. case "PSUCCESS":
  124. Toast.makeText(context, "Login Successfully....Parent", Toast.LENGTH_SHORT).show();
  125. ParentLogin();
  126. break;
  127. case "NSUCCESS":
  128. Toast.makeText(context, "Login Successfully....Nanny", Toast.LENGTH_SHORT).show();
  129. NannyLogin();
  130. break;
  131. case "FAILURE":
  132. Toast.makeText(context, "Wrong Data. Login failed.", Toast.LENGTH_SHORT).show();
  133. break;
  134. default:
  135. Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
  136. break;
  137. }
  138. } catch (JSONException e) {
  139. e.printStackTrace();
  140. Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
  141. }
  142. } else {
  143. Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
  144. }
  145. }
  146. }
  147.  
  148. public String job;
  149. public String username;
  150. public String password;
  151. TextView qwe;
  152.  
  153. @Override
  154. public void onCreate(Bundle savedInstanceState) {
  155. super.onCreate(savedInstanceState);
  156. setContentView(R.layout.activity_main);
  157. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  158. setSupportActionBar(toolbar);
  159.  
  160. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  161. fab.setOnClickListener(new View.OnClickListener() {
  162. @Override
  163. public void onClick(View view) {
  164. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  165. .setAction("Action", null).show();
  166. }
  167. });
  168. qwe = (TextView)findViewById(R.id.asd);
  169. Intent intent = new Intent();
  170. String strValue = intent.getStringExtra("class");
  171. String strValue1 = intent.getStringExtra("username");
  172. String strValue2 = intent.getStringExtra("password");
  173. qwe.setText(strValue);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement