Advertisement
Guest User

Login.java

a guest
Apr 25th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. package com.coba.aplikasiku;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.util.Patterns;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.net.HttpURLConnection;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import android.net.Uri;
  21.  
  22. import com.basgeekball.awesomevalidation.AwesomeValidation;
  23. import com.basgeekball.awesomevalidation.ValidationStyle;
  24.  
  25. import java.io.BufferedWriter;
  26. import java.io.OutputStream;
  27. import java.io.OutputStreamWriter;
  28.  
  29.  
  30. public class Login extends Activity {
  31.  
  32. // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
  33.  
  34. public static final int CONNECTION_TIMEOUT=10000;
  35. public static final int READ_TIMEOUT=15000;
  36. private EditText etEmail;
  37. private EditText etPassword;
  38. private Button btnLogin;
  39.  
  40. private AwesomeValidation awesomeValidation;
  41.  
  42. @Override
  43. protected void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.activity_login);
  46.  
  47. // Get Reference to variables
  48. etEmail = (EditText) findViewById(R.id.et_email);
  49. etPassword = (EditText) findViewById(R.id.et_password);
  50. btnLogin = (Button) findViewById(R.id.btn_login);
  51.  
  52. awesomeValidation = new AwesomeValidation(ValidationStyle.BASIC);
  53.  
  54. //adding validation to edittexts
  55. awesomeValidation.addValidation(this, R.id.et_email, Patterns.EMAIL_ADDRESS, R.string.emailerror);
  56. awesomeValidation.addValidation(this, R.id.et_password, "^[A-Za-z\\s]{1,}[\\.]{0,1}[A-Za-z\\s]{0,}$", R.string.passworderror);
  57.  
  58. btnLogin.setOnClickListener(new View.OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61.  
  62. if (awesomeValidation.validate()) {
  63.  
  64. // Get text from email and passord field
  65. final String email = etEmail.getText().toString();
  66. final String password = etPassword.getText().toString();
  67.  
  68. // Initialize AsyncLogin() class with email and password
  69. new AsyncLogin().execute(email,password);
  70. }
  71.  
  72. }
  73. });
  74.  
  75. }
  76.  
  77. private class AsyncLogin extends AsyncTask<String, String, String> {
  78. ProgressDialog pdLoading = new ProgressDialog(Login.this);
  79. HttpURLConnection conn;
  80. URL url = null;
  81.  
  82. @Override
  83. protected void onPreExecute() {
  84. super.onPreExecute();
  85.  
  86. //this method will be running on UI thread
  87. pdLoading.setMessage("\tMohon Ditunggu ...");
  88. pdLoading.setCancelable(false);
  89. pdLoading.show();
  90.  
  91. }
  92. @Override
  93. protected String doInBackground(String... params) {
  94. try {
  95.  
  96. // Enter URL address where your php file resides
  97. url = new URL("http://alamathosting.com/code/login.php");
  98.  
  99. } catch (MalformedURLException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. return "exception";
  103. }
  104. try {
  105. // Setup HttpURLConnection class to send and receive data from php and mysql
  106. conn = (HttpURLConnection)url.openConnection();
  107. conn.setReadTimeout(READ_TIMEOUT);
  108. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  109. conn.setRequestMethod("POST");
  110.  
  111. // setDoInput and setDoOutput method depict handling of both send and receive
  112. conn.setDoInput(true);
  113. conn.setDoOutput(true);
  114.  
  115. // Append parameters to URL
  116. Uri.Builder builder = new Uri.Builder()
  117. .appendQueryParameter("username", params[0])
  118. .appendQueryParameter("password", params[1]);
  119. String query = builder.build().getEncodedQuery();
  120.  
  121. // Open connection for sending data
  122. OutputStream os = conn.getOutputStream();
  123. BufferedWriter writer = new BufferedWriter(
  124. new OutputStreamWriter(os, "UTF-8"));
  125. writer.write(query);
  126. writer.flush();
  127. writer.close();
  128. os.close();
  129. conn.connect();
  130.  
  131. } catch (IOException e1) {
  132. // TODO Auto-generated catch block
  133. e1.printStackTrace();
  134. return "exception";
  135. }
  136.  
  137. try {
  138.  
  139. int response_code = conn.getResponseCode();
  140.  
  141. // Check if successful connection made
  142. if (response_code == HttpURLConnection.HTTP_OK) {
  143.  
  144. // Read data sent from server
  145. InputStream input = conn.getInputStream();
  146. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  147. StringBuilder result = new StringBuilder();
  148. String line;
  149.  
  150. while ((line = reader.readLine()) != null) {
  151. result.append(line);
  152. }
  153.  
  154. // Pass data to onPostExecute method
  155. return(result.toString());
  156.  
  157. }else{
  158.  
  159. return("unsuccessful");
  160. }
  161.  
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. return "exception";
  165. } finally {
  166. conn.disconnect();
  167. }
  168.  
  169. }
  170.  
  171. @Override
  172. protected void onPostExecute(String result) {
  173.  
  174. //this method will be running on UI thread
  175. pdLoading.dismiss();
  176.  
  177. if(result.equalsIgnoreCase("true")){
  178. /* Here launching another activity when login successful. If you persist login state
  179. use sharedPreferences of Android. and logout button to clear sharedPreferences.
  180. */
  181.  
  182. Intent intent = new Intent(Login.this,MainActivity.class);
  183. startActivity(intent);
  184. Login.this.finish();
  185.  
  186. }else if (result.equalsIgnoreCase("false")){
  187.  
  188. // If username and password does not match display a error message
  189. Toast.makeText(Login.this, "Username dan Password tidak sesuai", Toast.LENGTH_LONG).show();
  190.  
  191. } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
  192.  
  193. Toast.makeText(Login.this, "Terjadi Kesalahan. Periksa Koneksi Anda.", Toast.LENGTH_LONG).show();
  194.  
  195. }
  196. }
  197.  
  198. }
  199.  
  200.  
  201. public void onClickRegister(View theButton) {
  202. Intent register = new Intent(this, Register.class);
  203. startActivity(register);
  204. }
  205.  
  206. public void onClickForgot(View theButton) {
  207. Intent forgot = new Intent(this, ForgotPass.class);
  208. startActivity(forgot);
  209. }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement