Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7.  
  8.  
  9. public class PatLogin extends Activity {
  10.  
  11. EditText a,b;
  12. String login_name,login_pass;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.pat_login);
  17.  
  18. }
  19.  
  20. public void patbuttonClick(View v) {
  21. if (v.getId() == R.id.patlogin) {
  22. a = (EditText) findViewById(R.id.TFpusername);
  23. b = (EditText) findViewById(R.id.TFppassword);
  24. login_name = a.getText().toString();
  25. login_pass = b.getText().toString();
  26.  
  27.  
  28. String method = "login";
  29. BackgroundTask backgroundTask = new BackgroundTask(this);
  30. backgroundTask.execute(method,login_name,login_pass);
  31.  
  32. //If possible I would like to call the "Display" activity from here but only when the correct username and password is entered.
  33. //If it's not possible to call from here then I would like to know how to call "Display" activity from "BackgrounTask.java".
  34.  
  35.  
  36.  
  37.  
  38.  
  39. }
  40.  
  41. }
  42. }
  43.  
  44. import android.app.AlertDialog;
  45. import android.content.Context;
  46. import android.content.Intent;
  47. import android.os.AsyncTask;
  48. import android.widget.Toast;
  49. import java.io.BufferedReader;
  50. import java.io.BufferedWriter;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.InputStreamReader;
  54. import java.io.OutputStream;
  55. import java.io.OutputStreamWriter;
  56. import java.net.HttpURLConnection;
  57. import java.net.MalformedURLException;
  58. import java.net.URL;
  59. import java.net.URLEncoder;
  60.  
  61. public class BackgroundTask extends AsyncTask<String,Void,String> {
  62. AlertDialog alertDialog;
  63. Context ctx;
  64.  
  65. BackgroundTask(Context ctx)
  66. {
  67. this.ctx =ctx;
  68. }
  69. int flag=0;
  70. @Override
  71. protected void onPreExecute() {
  72. alertDialog = new AlertDialog.Builder(ctx).create();
  73. alertDialog.setTitle("Login Information....");
  74. }
  75. @Override
  76. protected String doInBackground(String... params) {
  77.  
  78. String login_url = "http://10.0.2.2/mobidoc/login.php";
  79. String method = params[0];
  80.  
  81. if(method.equals("login"))
  82. {
  83. String login_name = params[1];
  84. String login_pass = params[2];
  85. try {
  86. URL url = new URL(login_url);
  87. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  88. httpURLConnection.setRequestMethod("POST");
  89. httpURLConnection.setDoOutput(true);
  90. httpURLConnection.setDoInput(true);
  91. OutputStream outputStream = httpURLConnection.getOutputStream();
  92. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  93. String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
  94. URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
  95. bufferedWriter.write(data);
  96. bufferedWriter.flush();
  97. bufferedWriter.close();
  98. outputStream.close();
  99. InputStream inputStream = httpURLConnection.getInputStream();
  100. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  101. String response = "";
  102. String line = "";
  103. while ((line = bufferedReader.readLine())!=null)
  104. {
  105. response+= line;
  106. }
  107. bufferedReader.close();
  108. inputStream.close();
  109. httpURLConnection.disconnect();
  110. return response;
  111. } catch (MalformedURLException e) {
  112. e.printStackTrace();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. return null;
  118. }
  119. @Override
  120. protected void onProgressUpdate(Void... values) {
  121. super.onProgressUpdate(values);
  122. }
  123. @Override
  124. protected void onPostExecute(String result) {
  125. if(result.equals("Registration Success..."))
  126. {
  127. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  128. }
  129. else
  130. {
  131. alertDialog.setMessage(result);
  132. alertDialog.show();
  133. //When I use the following 2 lines of code, It calls the Display activity even if the wrong password is entered. I need a certain condition to be applied.
  134. Intent myIntent = new Intent(ctx, Display.class);
  135. ctx.startActivity(myIntent);
  136. }
  137. }
  138.  
  139.  
  140. }
  141.  
  142. <?php
  143.  
  144. require "init.php";
  145. $username = $_POST["login_name"];
  146. $password = $_POST["login_pass"];
  147. $password = md5($password);
  148. $sql_query = "select name from pat_info where username like '$username' and password like '$password';";
  149.  
  150. $result = mysqli_query($con,$sql_query);
  151.  
  152. if(mysqli_num_rows($result)>0)
  153. {
  154. $row = mysqli_fetch_assoc($result);
  155.  
  156. $name = $row["name"];
  157. echo "Login Success... Welcome ".$name;}
  158.  
  159. else{
  160. echo "Login Failed...Try Again.";
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement