Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. public class SigninActivity extends AsyncTask<String,Void,String> {
  2.  
  3. private TextView statusField,roleField;
  4. private Context context;
  5. private int byGetOrPost = 0;
  6. //flag 0 means get and 1 means post.(By default it is get.)
  7. public SigninActivity(Context context,TextView statusField,
  8. TextView roleField,int flag) {
  9. this.context = context;
  10. this.statusField = statusField;
  11. this.roleField = roleField;
  12. byGetOrPost = flag;
  13. }
  14.  
  15. protected void onPreExecute(){
  16.  
  17. }
  18. @Override
  19. protected String doInBackground(String... arg0) {
  20. if(byGetOrPost == 0){ //means by Get Method
  21. try{
  22. String username = (String)arg0[0];
  23. String password = (String)arg0[1];
  24. String link = "http://www.example.com/login.php?username="
  25. +username+"&password="+password;
  26. URL url = new URL(link);
  27. HttpClient client = new DefaultHttpClient();
  28. HttpGet request = new HttpGet();
  29. request.setURI(new URI(link));
  30. HttpResponse response = client.execute(request);
  31. BufferedReader in = new BufferedReader
  32. (new InputStreamReader(response.getEntity().getContent()));
  33.  
  34. StringBuffer sb = new StringBuffer("");
  35. String line="";
  36. while ((line = in.readLine()) != null) {
  37. sb.append(line);
  38. break;
  39. }
  40. in.close();
  41. return sb.toString();
  42. }catch(Exception e){
  43. return new String("Exception: " + e.getMessage());
  44. }
  45. }
  46. else{
  47. try{
  48. String username = (String)arg0[0];
  49. String password = (String)arg0[1];
  50. String link="http://www.example.com/login.php";
  51. String data = URLEncoder.encode("username", "UTF-8")
  52. + "=" + URLEncoder.encode(username, "UTF-8");
  53. data += "&" + URLEncoder.encode("password", "UTF-8")
  54. + "=" + URLEncoder.encode(password, "UTF-8");
  55. URL url = new URL(link);
  56. URLConnection conn = url.openConnection();
  57. conn.setDoOutput(true);
  58. OutputStreamWriter wr = new OutputStreamWriter
  59. (conn.getOutputStream());
  60. wr.write( data );
  61. wr.flush();
  62. BufferedReader reader = new BufferedReader
  63. (new InputStreamReader(conn.getInputStream()));
  64. StringBuilder sb = new StringBuilder();
  65. String line = null;
  66. // Read Server Response
  67. while((line = reader.readLine()) != null)
  68. {
  69. sb.append(line);
  70. break;
  71. }
  72. return sb.toString();
  73. }catch(Exception e){
  74. return new String("Exception: " + e.getMessage());
  75. }
  76. }
  77. }
  78. @Override
  79. protected void onPostExecute(String result){
  80. this.statusField.setText("Login Successful");
  81. this.roleField.setText(result);
  82. }
  83. }
  84.  
  85.  
  86. // Code in PHP
  87.  
  88. $con = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
  89.  
  90.  
  91.  
  92. if (mysqli_connect_errno($con))
  93. {
  94. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  95. }
  96.  
  97.  
  98. mysqli_select_db($con, $mysql_database) or die("Could not select database");
  99.  
  100.  
  101. if (DEBUG) {
  102. echo "Connected to database $mysql_database";
  103. }
  104.  
  105.  
  106. mysqli_set_charset($con, 'utf8');
  107. $username = mysqli_real_escape_string($con, $_POST['username']);
  108. $password = mysqli_real_escape_string($con, $_POST['password']);
  109.  
  110. if (DEBUG) {
  111. $password = "b";
  112. } else {$password = $_POST['password'];
  113. }
  114.  
  115. $qry1 = "SELECT c_f_name FROM name where username = "$username" ";
  116.  
  117. $result = mysqli_query($con, $qry1);
  118. if(DEBUG) {
  119. echo "This is the username: ";
  120. echo $username;
  121.  
  122. if ($result) {
  123. echo $username;
  124. //successful query
  125. if (mysqli_num_rows($result) > 0) {
  126. echo "query successful -
  127. ";
  128. echo mysqli_num_rows($result);
  129. } else {
  130. echo "query successful-0 rows
  131. ";
  132. }
  133. } else {
  134. // query not successful
  135. echo "Query unsuccessful";
  136. }
  137.  
  138. }
  139.  
  140. $row = mysqli_fetch_array($result);
  141. //Since there is only 1 record that match, just retrieve the first and only record
  142. $data = $row[0];
  143. if($data){
  144. echo $data;
  145. }
  146.  
  147. mysqli_close($con);
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement