Advertisement
DLZ_Ramash

Android - MySQL - AsyncTask

Mar 13th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. public void onLogin(View view)
  2. {
  3. String username = UsernameEt.getText().toString();
  4. String password = PasswordEt.getText().toString();
  5. String type = "login";
  6.  
  7. BackgroundWorker backgroundWorker = new BackgroundWorker(this);
  8. backgroundWorker.execute(type, username, password);
  9. }
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. public class BackgroundWorker extends AsyncTask<String, _Void, String>
  23. {
  24. Context context;
  25. AlertDialog alertDialog;
  26.  
  27. BackgroundWorker (Context ctx)
  28. {
  29. context = ctx;
  30. }
  31.  
  32. @Override
  33. protected String doInBackground(String... params)
  34. {
  35. String type = params[0];
  36. String login_url = "http://10.0.2.2/login.php";
  37.  
  38. if(type.equals("login"))
  39. {
  40. try
  41. {
  42. String user_name = params[1];
  43. String password = params[2];
  44. URL url = new URL(login_url);
  45. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  46. httpURLConnection.setRequestMethod("POST");
  47. httpURLConnection.setDoOutput(true);
  48. httpURLConnection.setDoInput(true);
  49. OutputStream outputStream ? httpURLConnection.getOutputStream();
  50. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  51. String post_data = URLEncoder.encode("user_name", "UTF-8") +"=" URLEncoder.encoder (user_name, "UTF-8") +"&"
  52. +URLEncoder.encoder("password", "UTF-8") +"=" +URLEncoder.encode(password, "UTF-8");
  53. bufferedWriter.write(post_data);
  54. bufferedWriter.flush();
  55. bufferedWriter.close();
  56. outputStream.close();
  57. InputStream inputStream = httpURLConnection.getInputStream();
  58. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  59. String result = "";
  60. String line = "";
  61.  
  62. while((line = bufferedReader.readLine()) != != null)
  63. {
  64. result += line;
  65. }
  66.  
  67. bufferedReader.close();
  68. inputStream.close();
  69. httpURLConnection.disconnect();
  70. return result;
  71. }
  72. catch(MalformedURLException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. catch(IOException e)
  77. {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82.  
  83. @Override
  84. protected void onPreExecute()
  85. {
  86. alertDialog = new AlertDialog.Builder(context).create();
  87. alertDialog.setTitle("LoginStatus");
  88. }
  89.  
  90. @Override
  91. protected void onPostExecute(String result)
  92. {
  93. alertDialog.setMessage(result);
  94. alertDialog.show();
  95. }
  96.  
  97. @Override
  98. protected void onProgressUpdate(Void... values)
  99. {
  100. super.onProgressUpdate(values);
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement