selvalives

Untitled

Dec 12th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. package com.nus.app1;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.net.Uri;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.Toast;
  18.  
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.BufferedWriter;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.io.OutputStream;
  28. import java.io.OutputStreamWriter;
  29. import java.net.HttpURLConnection;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32.  
  33. public class LoginActivity extends Activity
  34. {
  35. Button btnlogin;
  36. EditText tusername,tpassword;
  37. public static final int CONNECTION_TIMEOUT = 10000;
  38. public static final int READ_TIMEOUT = 150000;
  39.  
  40. SharedPreferences sharedpreferences;
  41. public static final String serverpref = "serverpref";
  42. public static final String serveraddress = "serveraddress";
  43. private String saddress;
  44. private String url_login;
  45. public void onCreate(Bundle b)
  46. {
  47. super.onCreate(b);
  48. setContentView(R.layout.loginlayout);
  49. btnlogin=findViewById(R.id.btnlogin);
  50. tusername=findViewById(R.id.tusername);
  51. tpassword=findViewById(R.id.tpassword);
  52. sharedpreferences = getSharedPreferences(serverpref, Context.MODE_PRIVATE);
  53. btnlogin.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56. if(tusername.getText().length()==0 || tpassword.getText().length()==0)
  57. {
  58. Toast.makeText(getApplicationContext(),"Username or Password is empty",Toast.LENGTH_LONG).show();
  59. }
  60. else
  61. {
  62. if (!sharedpreferences.contains(serveraddress) || sharedpreferences.getString(serveraddress,null)== null || sharedpreferences.getString(serveraddress,null)=="" )
  63. {
  64. Toast.makeText(getApplicationContext(),"Please set your server address here",Toast.LENGTH_LONG).show();
  65. Intent serveraddressintent=new Intent(getApplicationContext(),ServerAddressActivity.class);
  66. startActivity(serveraddressintent);
  67. }
  68. else
  69. {
  70. saddress=sharedpreferences.getString(serveraddress, "");
  71. url_login="http://"+saddress+"/servercode/loginprocess.php";
  72. new AsyncLogin().execute(tusername.getText().toString(), tpassword.getText().toString());
  73. }
  74.  
  75. }
  76. }
  77. });
  78. }
  79. public boolean onCreateOptionsMenu(Menu menu)
  80. {
  81. super.onCreateOptionsMenu(menu);
  82. MenuInflater menuInflater=getMenuInflater();
  83. menuInflater.inflate(R.menu.mymenu,menu);
  84. return true;
  85. }
  86. public boolean onOptionsItemSelected(MenuItem menuItem)
  87. {
  88. switch (menuItem.getItemId())
  89. {
  90. case R.id.serveraddress:
  91. Intent intent=new Intent(getApplicationContext(),ServerAddressActivity.class);
  92. startActivity(intent);
  93. return true;
  94. default:
  95. return super.onContextItemSelected(menuItem);
  96. }
  97. }
  98. private class AsyncLogin extends AsyncTask<String, String, String>
  99. {
  100. ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this);
  101. HttpURLConnection conn;
  102. URL url = null;
  103.  
  104. @Override
  105. protected void onPreExecute()
  106. {
  107. super.onPreExecute();
  108. pdLoading.setMessage("\tLoading...");
  109. pdLoading.setCancelable(false);
  110. pdLoading.show();
  111. }
  112. @Override
  113. protected String doInBackground(String... params)
  114. {
  115. try
  116. {
  117. url = new URL(url_login);
  118. }
  119. catch (MalformedURLException e)
  120. {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. return "exception";
  124. }
  125. try
  126. {
  127. conn = (HttpURLConnection)url.openConnection();
  128. conn.setReadTimeout(READ_TIMEOUT);
  129. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  130. conn.setRequestMethod("POST");
  131. conn.setDoInput(true);
  132. conn.setDoOutput(true);
  133. Uri.Builder builder = new Uri.Builder()
  134. .appendQueryParameter("username", params[0])
  135. .appendQueryParameter("password", params[1]);
  136. String query = builder.build().getEncodedQuery();
  137. OutputStream os = conn.getOutputStream();
  138. BufferedWriter writer = new BufferedWriter(
  139. new OutputStreamWriter(os, "UTF-8"));
  140. writer.write(query);
  141. writer.flush();
  142. writer.close();
  143. os.close();
  144. conn.connect();
  145. }
  146. catch (IOException e1)
  147. {
  148. e1.printStackTrace();
  149. return "exception";
  150. }
  151. try
  152. {
  153. int response_code = conn.getResponseCode();
  154. if (response_code == HttpURLConnection.HTTP_OK)
  155. {
  156. InputStream input = conn.getInputStream();
  157. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  158. StringBuilder result = new StringBuilder();
  159. String line;
  160. while ((line = reader.readLine()) != null)
  161. {
  162. result.append(line);
  163. }
  164.  
  165. return(result.toString());
  166. }
  167. else
  168. {
  169. return("unsuccessful");
  170. }
  171.  
  172. } catch (IOException e)
  173. {
  174. e.printStackTrace();
  175. return "exception";
  176. }
  177. finally {
  178. conn.disconnect();
  179. }
  180. }
  181. @Override
  182. protected void onPostExecute(String result)
  183. {
  184. pdLoading.cancel();
  185. try
  186. {
  187. JSONObject json=new JSONObject(result);
  188. if(json.getString("status").equals("true"))
  189. {
  190. String fullname = json.getString("fullname");
  191. //editor.putString("fullname", json.getString("fullname"));
  192. Toast.makeText(LoginActivity.this, "Login Success...", Toast.LENGTH_LONG).show();
  193. Intent intent = new Intent(LoginActivity.this,MenuActivity.class);
  194. startActivity(intent);
  195. LoginActivity.this.finish();
  196. }
  197. else if(json.getString("status").equals("false"))
  198. {
  199. Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
  200. }
  201. else
  202. {
  203. Toast.makeText(LoginActivity.this, "Please check server connection", Toast.LENGTH_LONG).show();
  204. }
  205. }
  206. catch (JSONException e)
  207. {
  208. e.printStackTrace();
  209. }
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment