Guest User

Untitled

a guest
Mar 7th, 2018
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package com.abc.intech1;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. /**
  20. * Created by User on 3/7/2018.
  21. */
  22.  
  23. public class loginactivity extends Activity {
  24.  
  25. //btnlogin here is an object (same name as button id from layout)
  26. Button btnlogin;
  27. EditText tusername, tpassword;
  28. TextView msg;
  29. String username, password;
  30.  
  31. ProgressDialog pDialog;
  32. JSONParser jsonParser = new JSONParser();
  33. private static final String url_login = "http://10.206.1.70/xamarin/loginprocess.php";
  34.  
  35. public void onCreate(Bundle b) {
  36. super.onCreate(b);
  37. setContentView(R.layout.loginlayout);
  38. //mapping
  39. btnlogin = findViewById(R.id.btnlogin);
  40. tusername = findViewById(R.id.tusername);
  41. tpassword = findViewById(R.id.tpassword);
  42. msg = findViewById(R.id.msg);
  43.  
  44. btnlogin.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. btnlogin.setText("clicked");
  48. username = tusername.getText().toString();
  49. password = tpassword.getText().toString();
  50.  
  51. if (username.length() == 0 || password.length() == 0) {
  52. msg.setText("Username or password is missing");
  53. }
  54. else {
  55. Intent menuintent = new Intent(getApplicationContext(), menuactivity.class);
  56.  
  57. }
  58. }
  59. });
  60. }
  61.  
  62. class getlogindetails extends AsyncTask<String, String, String>
  63. {
  64. protected void onPreExecute()
  65. {
  66. super.onPreExecute();
  67. pDialog = new ProgressDialog(loginactivity.this);
  68. pDialog.setMessage("Logging in. Please wait...");
  69. pDialog.setIndeterminate(false);
  70. pDialog.setCancelable(true);
  71. pDialog.show();
  72. }
  73. protected String doInBackground(String... params)
  74. {
  75. int success;
  76. try
  77. {
  78. Uri.Builder builder = new Uri.Builder()
  79. .appendQueryParameter("username", username)
  80. .appendQueryParameter("password", password);
  81. String query = builder.build().getEncodedQuery();
  82. JSONObject json = jsonParser.makeHttpRequest(url_login, query);
  83. if(json !=null)
  84. {
  85. Log.d("Login", json.toString());
  86. success = json.getInt("result");
  87. if (success == 1)
  88. {
  89. Intent myintent = new Intent(getApplicationContext(), menuactivity.class);
  90. startActivity(myintent);
  91. }
  92. else
  93. {
  94. loginactivity.this.runOnUiThread(new Runnable()
  95. {
  96. public void run()
  97. { //toast similar to alert in javascript
  98. Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_LONG).show();
  99. }
  100. });
  101. }
  102. }
  103. else
  104. {
  105. loginactivity.this.runOnUiThread(new Runnable()
  106. {
  107. public void run()
  108. {
  109. Toast.makeText(loginactivity.this,"Unable to contact server",Toast.LENGTH_LONG).show();
  110.  
  111. }
  112. });
  113. return null;
  114. }
  115. }
  116. catch (JSONException e)
  117. {
  118. e.printStackTrace();
  119. }
  120. return null;
  121. }
  122. protected void onPostExecute(String file_url)
  123. {
  124. pDialog.dismiss();
  125. }
  126. }
  127. }
Add Comment
Please, Sign In to add comment