Guest User

Untitled

a guest
Feb 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. import android.app.ProgressDialog;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.net.Uri;
  5. import android.os.AsyncTask;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. import java.io.BufferedReader;
  12. import java.io.BufferedWriter;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.io.OutputStream;
  17. import java.io.OutputStreamWriter;
  18. import java.net.HttpURLConnection;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24. public static final int CONNECTION_TIMEOUT = 10000;
  25. public static final int READ_TIMEOUT = 15000;
  26. private EditText etName;
  27. private EditText etPassword;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. etName = (EditText) findViewById(R.id.username);
  34. etPassword = (EditText) findViewById(R.id.password);
  35. }
  36.  
  37. /*public void openSales(View view){
  38. Intent i = new Intent(this, menuActivity.class);
  39. startActivity(i);
  40. }*/
  41. public void checkLogin(View arg0) {
  42.  
  43. final String name = etName.getText().toString();
  44. final String password = etPassword.getText().toString();
  45. new AsyncLogin().execute(name,password);
  46. }
  47. private class AsyncLogin extends AsyncTask<String, String, String> {
  48. ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
  49. HttpURLConnection conn;
  50. URL url = null;
  51.  
  52. @Override
  53. protected void onPreExecute() {
  54. super.onPreExecute();
  55. pdLoading.setMessage("tLoading...");
  56. pdLoading.setCancelable(false);
  57. pdLoading.show();
  58. }
  59.  
  60. @Override
  61. protected String doInBackground(String... params) {
  62. try {
  63. url = new URL("http://localhost/THESIS/database.inc.php");
  64. } catch (MalformedURLException e) {
  65. e.printStackTrace();
  66. return "exception";
  67. }
  68. try {
  69. conn = (HttpURLConnection) url.openConnection();
  70. conn.setReadTimeout(READ_TIMEOUT);
  71. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  72. conn.setRequestMethod("POST");
  73. conn.setDoInput(true);
  74. conn.setDoOutput(true);
  75. Uri.Builder builder = new Uri.Builder()
  76. .appendQueryParameter("username", params[0])
  77. .appendQueryParameter("password", params[1]);
  78. String query = builder.build().getEncodedQuery();
  79. OutputStream os = conn.getOutputStream();
  80. BufferedWriter writer = new BufferedWriter(
  81. new OutputStreamWriter(os, "UTF-8"));
  82. writer.write(query);
  83. writer.flush();
  84. writer.close();
  85. os.close();
  86. conn.connect();
  87.  
  88. } catch (IOException e1) {
  89. // TODO Auto-generated catch block
  90. e1.printStackTrace();
  91. return "exception";
  92. }
  93. try {
  94.  
  95. int response_code = conn.getResponseCode();
  96. if (response_code == HttpURLConnection.HTTP_OK) {
  97. InputStream input = conn.getInputStream();
  98. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  99. StringBuilder result = new StringBuilder();
  100. String line;
  101.  
  102. while ((line = reader.readLine()) != null) {
  103. result.append(line);
  104. }
  105. return (result.toString());
  106.  
  107. } else {
  108.  
  109. return ("unsuccessful");
  110. }
  111.  
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. return "exception";
  115. } finally {
  116. conn.disconnect();
  117. }
  118.  
  119.  
  120. }
  121.  
  122. @Override
  123. protected void onPostExecute(String result) {
  124. pdLoading.dismiss();
  125.  
  126. if (result.equalsIgnoreCase("true")) {
  127. Intent intent = new Intent(MainActivity.this, menuActivity.class);
  128. startActivity(intent);
  129. MainActivity.this.finish();
  130.  
  131. } else if (result.equalsIgnoreCase("false")) {
  132. Toast.makeText(MainActivity.this, "Invalid email or password", Toast.LENGTH_LONG).show();
  133.  
  134. } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
  135.  
  136. Toast.makeText(MainActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
  137. }
  138. }
  139. }
  140. }
  141.  
  142. $servername = "db4free.net:3307/******";
  143. $username = "*****";
  144. $password = "*********";
  145. $dbname = "*******";
  146.  
  147. try {[enter image description here][1]
  148. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  149. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  150. }
  151. catch(PDOException $e)
  152. {
  153. die("OOPs something went wrong");
  154. }
  155. if(isset($_POST['username']) && isset($_POST['password']))
  156. {
  157. // Innitialize Variable
  158. $result='';
  159. $username = $_POST['username'];
  160. $password = $_POST['password'];
  161.  
  162. // Query database for row exist or not
  163. $sql = 'SELECT * FROM asd WHERE name = :username AND pass = :password';
  164. $stmt = $conn->prepare($sql);
  165. $stmt->bindParam(':username', $username, PDO::PARAM_STR);
  166. $stmt->bindParam(':password', $password, PDO::PARAM_STR);
  167. $stmt->execute();
  168. if($stmt->rowCount())
  169. {
  170. $result="true";
  171. }
  172. elseif(!$stmt->rowCount())
  173. {
  174. $result="false";
  175. }
  176.  
  177. // send result back to android
  178. echo $result;
  179. }
Add Comment
Please, Sign In to add comment