Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. public class MainActivity extends ActionBarActivity {
  2.  
  3. EditText UsernameEt, PasswordEt;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. UsernameEt = (EditText)findViewById(R.id.etUserName);
  9. PasswordEt = (EditText)findViewById(R.id.etPassword);
  10.  
  11. }
  12.  
  13.  
  14. public void OnLogin(View view) {
  15. String username = UsernameEt.getText().toString();
  16. String password = PasswordEt.getText().toString();
  17. String type = "login";
  18. BackgroundWorker backgroundWorker = new BackgroundWorker(this);
  19. backgroundWorker.execute(type, username, password);
  20.  
  21.  
  22.  
  23. }
  24.  
  25. }
  26.  
  27. public class BackgroundWorker extends AsyncTask<String,Void,String> {
  28.  
  29. Context context;
  30.  
  31. AlertDialog alertDialog;
  32.  
  33. BackgroundWorker (Context ctx) {
  34. context = ctx;
  35.  
  36. }
  37.  
  38. @Override
  39. protected String doInBackground(String... params) {
  40. String type = params[0];
  41.  
  42. String login_url = "http://10.127.127.1/ws/login.php";
  43.  
  44. if(type.equals("login")) {
  45. try {
  46.  
  47. String user_name = params[1];
  48. String password = params[2];
  49. URL url = new URL(login_url);
  50. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  51. httpURLConnection.setRequestMethod("POST");
  52. httpURLConnection.setDoOutput(true);
  53. httpURLConnection.setDoInput(true);
  54. OutputStream outputStream = httpURLConnection.getOutputStream();
  55. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  56. String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
  57. +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  58. bufferedWriter.write(post_data);
  59. bufferedWriter.flush();
  60. bufferedWriter.close();
  61. outputStream.close();
  62. InputStream inputStream = httpURLConnection.getInputStream();
  63. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  64. String result="";
  65. String line="";
  66.  
  67. while((line = bufferedReader.readLine())!= null) {
  68. result += line;
  69. }
  70.  
  71. bufferedReader.close();
  72. inputStream.close();
  73. httpURLConnection.disconnect();
  74. return result;
  75.  
  76.  
  77. } catch (MalformedURLException e) {
  78. e.printStackTrace();
  79.  
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82.  
  83. }
  84. }
  85.  
  86.  
  87. return null;
  88. }
  89.  
  90.  
  91. @Override
  92. protected void onPreExecute() {
  93. alertDialog = new AlertDialog.Builder(context).create();
  94. alertDialog.setTitle("Status do login");
  95.  
  96. }
  97.  
  98. @Override
  99. protected void onPostExecute(String result) {
  100. alertDialog.setMessage(result);
  101. alertDialog.show();
  102.  
  103. }
  104.  
  105. @Override
  106. protected void onProgressUpdate(Void... values) {
  107. super.onProgressUpdate(values);
  108. }
  109.  
  110.  
  111. }
  112.  
  113. <?php
  114. require "conn.php";
  115. $user_name = $_POST["user_name"];
  116. $user_pass = $_POST["password"];
  117. $mysql_qry = "select * from login where username like '$user_name' and password like '$user_pass';";
  118. $result = mysqli_query($conn ,$mysql_qry);
  119. if(mysqli_num_rows($result) > 0) {
  120. echo "login success !!!!! Welcome user";
  121. }
  122. else {
  123. echo "login not success";
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement