Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. import android.app.AlertDialog;
  2. import android.content.Context;
  3. import android.os.AsyncTask;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16.  
  17. public class BackgroundWorker extends AsyncTask<String,Void,String> {
  18. Context context;
  19. AlertDialog alertDialog;
  20. BackgroundWorker (Context ctx) {
  21. context = ctx;
  22. }
  23.  
  24.  
  25.  
  26. @Override
  27. protected String doInBackground(String... params) {
  28. String type = params[0];
  29. String login_url = "xxx";
  30. String register_url = "xxx";
  31. if (type.equals("login")) {
  32. alertDialog.setTitle("3");
  33. try {
  34. alertDialog.setTitle("4");
  35. String user_name = params[1];
  36. String password = params[2];
  37. URL url = new URL(login_url);
  38. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  39. httpURLConnection.setRequestMethod("POST");
  40. httpURLConnection.setDoOutput(true);
  41. httpURLConnection.setDoInput(true);
  42. OutputStream outputStream = httpURLConnection.getOutputStream();
  43. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  44. String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
  45. + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  46. bufferedWriter.write(post_data);
  47. bufferedWriter.flush();
  48. bufferedWriter.close();
  49. outputStream.close();
  50. InputStream inputStream = httpURLConnection.getInputStream();
  51. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  52. String result = "";
  53. String line = "";
  54. while ((line = bufferedReader.readLine()) != null) {
  55. result += line;
  56. }
  57. bufferedReader.close();
  58. inputStream.close();
  59. httpURLConnection.disconnect();
  60. return result;
  61. } catch (MalformedURLException e) {
  62. e.printStackTrace();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. } else if (type.equals("register")) {
  67. alertDialog.setTitle("1");
  68. try {
  69. alertDialog.setTitle("2");
  70. String username = params[1];
  71. String password = params[2];
  72. URL url = new URL(register_url);
  73. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  74. httpURLConnection.setRequestMethod("POST");
  75. httpURLConnection.setDoOutput(true);
  76. httpURLConnection.setDoInput(true);
  77. OutputStream outputStream = httpURLConnection.getOutputStream();
  78. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  79. String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
  80. + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password , "UTF-8");
  81. bufferedWriter.write(post_data);
  82. bufferedWriter.flush();
  83. bufferedWriter.close();
  84. outputStream.close();
  85. InputStream inputStream = httpURLConnection.getInputStream();
  86. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  87. String result = "";
  88. String line = "";
  89. while ((line = bufferedReader.readLine()) != null) {
  90. result += line;
  91. }
  92. bufferedReader.close();
  93. inputStream.close();
  94. httpURLConnection.disconnect();
  95. return result;
  96. } catch (MalformedURLException e) {
  97. e.printStackTrace();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102.  
  103. return null;
  104. }
  105.  
  106. @Override
  107. protected void onPreExecute() {
  108. alertDialog = new AlertDialog.Builder(context).create();
  109. alertDialog.setTitle("Login Status");
  110. }
  111.  
  112. @Override
  113. protected void onPostExecute(String result) {
  114. alertDialog.setMessage(result);
  115. alertDialog.show();
  116. }
  117.  
  118. @Override
  119. protected void onProgressUpdate(Void... values) {
  120. super.onProgressUpdate(values);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement