Advertisement
didik2017

Login sesion

Apr 15th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. package app.crud;
  2.  
  3. /**
  4. * Created by sEmraWutt on 4/14/2017.
  5. */
  6. import android.app.Dialog;
  7. import android.app.ProgressDialog;
  8. import android.content.Intent;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.support.v7.app.ActionBarActivity;
  12. import android.view.Menu;
  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.apache.http.HttpEntity;
  20. import org.apache.http.HttpResponse;
  21. import org.apache.http.NameValuePair;
  22. import org.apache.http.client.ClientProtocolException;
  23. import org.apache.http.client.HttpClient;
  24. import org.apache.http.client.entity.UrlEncodedFormEntity;
  25. import org.apache.http.client.methods.HttpPost;
  26. import org.apache.http.impl.client.DefaultHttpClient;
  27. import org.apache.http.message.BasicNameValuePair;
  28.  
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.InputStreamReader;
  33. import java.io.UnsupportedEncodingException;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36.  
  37.  
  38. public class LoginActivity extends ActionBarActivity implements View.OnClickListener {
  39.  
  40. private EditText editTextUserName;
  41. private EditText editTextPassword;
  42. private Button button;
  43.  
  44. public static final String USER_NAME = "USERNAME";
  45.  
  46. String username;
  47. String password;
  48.  
  49. @Override
  50. protected void onCreate(Bundle savedInstanceState) {
  51. super.onCreate(savedInstanceState);
  52. setContentView(R.layout.activity_login);
  53.  
  54. editTextUserName = (EditText) findViewById(R.id.nis);
  55. editTextPassword = (EditText) findViewById(R.id.input_password);
  56. button = (Button) findViewById(R.id.button);
  57. button.setOnClickListener(this);
  58. }
  59.  
  60. private void login(final String username, String password) {
  61.  
  62. class LoginAsync extends AsyncTask<String, Void, String>{
  63.  
  64. private Dialog loadingDialog;
  65.  
  66. @Override
  67. protected void onPreExecute() {
  68. super.onPreExecute();
  69. loadingDialog = ProgressDialog.show(LoginActivity.this, "Please wait", "Loading...");
  70. }
  71.  
  72. @Override
  73. protected String doInBackground(String... params) {
  74. String uname = params[0];
  75. String pass = params[1];
  76.  
  77. InputStream is = null;
  78. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  79. nameValuePairs.add(new BasicNameValuePair("username", uname));
  80. nameValuePairs.add(new BasicNameValuePair("password", pass));
  81. String result = null;
  82.  
  83. try{
  84. HttpClient httpClient = new DefaultHttpClient();
  85. HttpPost httpPost = new HttpPost(
  86. "http://172.16.5.134/monitor/php/login.php");
  87. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  88.  
  89. HttpResponse response = httpClient.execute(httpPost);
  90.  
  91. HttpEntity entity = response.getEntity();
  92.  
  93. is = entity.getContent();
  94.  
  95. BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
  96. StringBuilder sb = new StringBuilder();
  97.  
  98. String line = null;
  99. while ((line = reader.readLine()) != null)
  100. {
  101. sb.append(line + "\n");
  102. }
  103. result = sb.toString();
  104. } catch (ClientProtocolException e) {
  105. e.printStackTrace();
  106. } catch (UnsupportedEncodingException e) {
  107. e.printStackTrace();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. return result;
  112. }
  113.  
  114. @Override
  115. protected void onPostExecute(String result){
  116. String s = result.trim();
  117. loadingDialog.dismiss();
  118. if(s.equalsIgnoreCase("success")){
  119. Intent intent = new Intent(LoginActivity.this, ReadActivity.class);
  120. intent.putExtra(USER_NAME, username);
  121. finish();
  122. startActivity(intent);
  123. }else {
  124. Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
  125. }
  126. }
  127. }
  128.  
  129. LoginAsync la = new LoginAsync();
  130. la.execute(username, password);
  131.  
  132. }
  133.  
  134.  
  135. @Override
  136. public boolean onCreateOptionsMenu(Menu menu) {
  137. // Inflate the menu; this adds items to the action bar if it is present.
  138. getMenuInflater().inflate(R.menu.menu_refresh, menu);
  139. return true;
  140. }
  141.  
  142. @Override
  143. public boolean onOptionsItemSelected(MenuItem item) {
  144. // Handle action bar item clicks here. The action bar will
  145. // automatically handle clicks on the Home/Up button, so long
  146. // as you specify a parent activity in AndroidManifest.xml.
  147. int id = item.getItemId();
  148. return super.onOptionsItemSelected(item);
  149. }
  150.  
  151. @Override
  152. public void onClick(View v) {
  153. username = editTextUserName.getText().toString();
  154. password = editTextPassword.getText().toString();
  155. login(username,password);
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement