Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. package com.umapp;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  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.support.v7.app.AppCompatActivity;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.BufferedWriter;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.io.OutputStream;
  21. import java.io.OutputStreamWriter;
  22. import java.net.HttpURLConnection;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.  
  28. // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
  29.  
  30. public static final int CONNECTION_TIMEOUT=10000;
  31. public static final int READ_TIMEOUT=15000;
  32. private EditText etUsername;
  33. private EditText etPassword;
  34. public String loginu;
  35. char AdminAuthChar='A';
  36. char InstAuthChar='I';
  37. char StudentAuthChar='S';
  38. char AuthFirstChar;
  39. public static final String un="";
  40. public static final String psswd="";
  41. SharedPreferences sharedpreferences;
  42. String username = "";
  43. String password = "";
  44. public static final String MyPREFERENCES = "MyPrefs" ;
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50.  
  51. etUsername = (EditText) findViewById(R.id.username);
  52. etPassword = (EditText) findViewById(R.id.password);
  53. sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  54.  
  55. }
  56.  
  57.  
  58. // Triggers when LOGIN Button clicked
  59. public void checkLogin(View arg0) {
  60.  
  61. // Get text from username and password field
  62. username = etUsername.getText().toString();
  63. password = etPassword.getText().toString();
  64.  
  65. // Initialize AsyncLogin() class with username and password
  66. new AsyncLogin().execute(username,password);
  67. AuthFirstChar=username.charAt(0);
  68.  
  69. }
  70.  
  71. private class AsyncLogin extends AsyncTask<String, String, String>
  72. {
  73. ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
  74. HttpURLConnection conn;
  75. URL url = null;
  76.  
  77. @Override
  78. protected void onPreExecute() {
  79. super.onPreExecute();
  80.  
  81. //this method will be running on UI thread
  82. pdLoading.setMessage("\tLoading...");
  83. pdLoading.setCancelable(false);
  84. pdLoading.show();
  85.  
  86. }
  87. @Override
  88. protected String doInBackground(String... params) {
  89. try {
  90.  
  91. // Enter URL address where your php file resides
  92. url = new URL("http://146.185.173.181/login.inc.php");
  93.  
  94. } catch (MalformedURLException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. return "exception";
  98. }
  99. try {
  100. // Setup HttpURLConnection class to send and receive data from php and mysql
  101. conn = (HttpURLConnection)url.openConnection();
  102. conn.setReadTimeout(READ_TIMEOUT);
  103. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  104. conn.setRequestMethod("POST");
  105.  
  106. // setDoInput and setDoOutput method depict handling of both send and receive
  107. conn.setDoInput(true);
  108. conn.setDoOutput(true);
  109.  
  110. // Append parameters to URL
  111. Uri.Builder builder = new Uri.Builder()
  112. .appendQueryParameter("username", params[0])
  113. .appendQueryParameter("password", params[1]);
  114. String query = builder.build().getEncodedQuery();
  115.  
  116. // Open connection for sending data
  117. OutputStream os = conn.getOutputStream();
  118. BufferedWriter writer = new BufferedWriter(
  119. new OutputStreamWriter(os, "UTF-8"));
  120. writer.write(query);
  121. writer.flush();
  122. writer.close();
  123. os.close();
  124. conn.connect();
  125.  
  126. } catch (IOException e1) {
  127. // TODO Auto-generated catch block
  128. e1.printStackTrace();
  129. return "exception";
  130. }
  131.  
  132. try {
  133.  
  134. int response_code = conn.getResponseCode();
  135.  
  136. // Check if successful connection made
  137. if (response_code == HttpURLConnection.HTTP_OK) {
  138.  
  139. // Read data sent from server
  140. InputStream input = conn.getInputStream();
  141. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  142. StringBuilder result = new StringBuilder();
  143. String line;
  144.  
  145. while ((line = reader.readLine()) != null) {
  146. result.append(line);
  147. }
  148.  
  149. // Pass data to onPostExecute method
  150. return(result.toString());
  151.  
  152. }else{
  153.  
  154. return("unsuccessful");
  155. }
  156.  
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. return "exception";
  160. } finally {
  161. conn.disconnect();
  162. }
  163.  
  164.  
  165. }
  166.  
  167. @Override
  168. protected void onPostExecute(String result) {
  169.  
  170. //this method will be running on UI thread
  171.  
  172. pdLoading.dismiss();
  173.  
  174.  
  175. /* Here launching another activity when login successful. If you persist login state
  176. use sharedPreferences of Android. and logout button to clear sharedPreferences.
  177. */
  178. if(result.equalsIgnoreCase("true")&&AuthFirstChar==AdminAuthChar) {
  179. SharedPreferences.Editor editor = sharedpreferences.edit();
  180. Intent intent = new Intent(MainActivity.this, AdminPanel.class);
  181. etUsername = (EditText) findViewById(R.id.username);
  182. String username = etUsername.getText().toString();
  183. intent.putExtra("EXTRA_Username", username);
  184. editor.putString(un, username);
  185. editor.putString(psswd, password);
  186. editor.commit();
  187. startActivity(intent);
  188. MainActivity.this.finish();
  189. }
  190. else
  191. if(result.equalsIgnoreCase("true")&&AuthFirstChar==InstAuthChar) {
  192. SharedPreferences.Editor editor = sharedpreferences.edit();
  193. etUsername = (EditText) findViewById(R.id.username);
  194. String username = etUsername.getText().toString();
  195. Intent intent = new Intent(MainActivity.this, InstructorPanel.class);
  196. intent.putExtra("EXTRA_Username", username);
  197. editor.putString(un, username);
  198. editor.putString(psswd, password);
  199. editor.commit();
  200. startActivity(intent);
  201. MainActivity.this.finish();
  202. }
  203. else
  204. if(result.equalsIgnoreCase("true")&&AuthFirstChar==StudentAuthChar) {
  205. SharedPreferences.Editor editor = sharedpreferences.edit();
  206. Intent intent = new Intent(MainActivity.this, StudentPanel.class);
  207. etUsername = (EditText) findViewById(R.id.username);
  208. String username = etUsername.getText().toString();
  209. intent.putExtra("EXTRA_Username", username);
  210. startActivity(intent);
  211. editor.putString(un, username);
  212. editor.putString(psswd, password);
  213. editor.commit();
  214. MainActivity.this.finish();
  215. }
  216.  
  217. else if (result.equalsIgnoreCase("false")){
  218.  
  219. // If username and password does not match display a error message
  220. Toast.makeText(MainActivity.this, "Invalid Username or password", Toast.LENGTH_LONG);
  221.  
  222. } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
  223.  
  224. Toast.makeText(MainActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG);
  225.  
  226. }
  227. }
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement