Advertisement
selvalives

Untitled

Dec 12th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. package com.nsu.visitorsys;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.net.Uri;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.net.HttpURLConnection;
  26. import java.net.MalformedURLException;
  27. import java.net.URL;
  28.  
  29. public class loginactivity extends Activity
  30. {
  31. EditText tusername, tpassword;
  32. Button btnlogin;
  33. SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
  34. SharedPreferences.Editor editor = pref.edit();
  35. public static final int CONNECTION_TIMEOUT = 10000;
  36. public static final int READ_TIMEOUT = 150000;
  37. public void onCreate(Bundle b)
  38. {
  39. super.onCreate(b);
  40. setContentView(R.layout.loginlayout);
  41. tusername = findViewById(R.id.tusername);
  42. tpassword = findViewById(R.id.tpassword);
  43. btnlogin = findViewById(R.id.btnlogin);
  44. btnlogin.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. String username = tusername.getText().toString();
  48. String password = tpassword.getText().toString();
  49. if (username.length() == 0 || password.length() == 0)
  50. {
  51. Toast.makeText(getApplicationContext(),"username of password is missing", Toast.LENGTH_LONG).show();
  52. }
  53. else
  54. {
  55. new AsyncLogin().execute(tusername.getText().toString(), tpassword.getText().toString());
  56. }
  57. }
  58. });
  59. }
  60. private class AsyncLogin extends AsyncTask<String, String, String>
  61. {
  62. ProgressDialog pdLoading = new ProgressDialog(loginactivity.this);
  63. HttpURLConnection conn;
  64. URL url = null;
  65.  
  66. @Override
  67. protected void onPreExecute()
  68. {
  69. super.onPreExecute();
  70. pdLoading.setMessage("\tLoading...");
  71. pdLoading.setCancelable(false);
  72. pdLoading.show();
  73. }
  74. @Override
  75. protected String doInBackground(String... params)
  76. {
  77. try
  78. {
  79. url = new URL("http://172.25.96.213/NUSVisitorWeb/login.php");
  80. }
  81. catch (MalformedURLException e)
  82. {
  83. // TODO Auto-generated catch block
  84. e.printStackTrace();
  85. return "exception";
  86. }
  87. try
  88. {
  89. conn = (HttpURLConnection)url.openConnection();
  90. conn.setReadTimeout(READ_TIMEOUT);
  91. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  92. conn.setRequestMethod("POST");
  93. conn.setDoInput(true);
  94. conn.setDoOutput(true);
  95. Uri.Builder builder = new Uri.Builder()
  96. .appendQueryParameter("username", params[0])
  97. .appendQueryParameter("password", params[1]);
  98. String query = builder.build().getEncodedQuery();
  99. OutputStream os = conn.getOutputStream();
  100. BufferedWriter writer = new BufferedWriter(
  101. new OutputStreamWriter(os, "UTF-8"));
  102. writer.write(query);
  103. writer.flush();
  104. writer.close();
  105. os.close();
  106. conn.connect();
  107. }
  108. catch (IOException e1)
  109. {
  110. e1.printStackTrace();
  111. return "exception";
  112. }
  113. try
  114. {
  115. int response_code = conn.getResponseCode();
  116. if (response_code == HttpURLConnection.HTTP_OK)
  117. {
  118. InputStream input = conn.getInputStream();
  119. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  120. StringBuilder result = new StringBuilder();
  121. String line;
  122. while ((line = reader.readLine()) != null)
  123. {
  124. result.append(line);
  125. }
  126.  
  127. return(result.toString());
  128. }
  129. else
  130. {
  131. return("unsuccessful");
  132. }
  133.  
  134. } catch (IOException e)
  135. {
  136. e.printStackTrace();
  137. return "exception";
  138. }
  139. finally {
  140. conn.disconnect();
  141. }
  142. }
  143. @Override
  144. protected void onPostExecute(String result)
  145. {
  146. pdLoading.cancel();
  147. try
  148. {
  149. JSONObject json=new JSONObject(result);
  150. if(json.getString("status").equals("true"))
  151. {
  152. String fullname = json.getString("fullname");
  153. editor.putString("name", json.getString("fullname"));
  154. Toast.makeText(loginactivity.this, "Login Success...", Toast.LENGTH_LONG).show();
  155. Intent intent = new Intent(loginactivity.this,menuactivity.class);
  156. startActivity(intent);
  157. loginactivity.this.finish();
  158. }
  159. else if(json.getString("status").equals("false"))
  160. {
  161. Toast.makeText(loginactivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
  162. }
  163. else
  164. {
  165. Toast.makeText(loginactivity.this, "Please check server connection", Toast.LENGTH_LONG).show();
  166. }
  167. }
  168. catch (JSONException e)
  169. {
  170. e.printStackTrace();
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement