Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. public class BackgroundWorker extends AsyncTask<String,Void,String> {
  2. Context context;
  3. AlertDialog alertDialog;
  4. BackgroundWorker (Context ctx) {
  5. context = ctx;
  6. }
  7. @Override
  8. protected String doInBackground(String... params) {
  9. String type = params[0];
  10. String register_url = "http://xxxxx/register.php";
  11. if (type.equals("register")) {
  12. try {
  13. String username = params[1];
  14. String password = params[2];
  15. URL url = new URL(register_url);
  16. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  17. httpURLConnection.setRequestMethod("POST");
  18. httpURLConnection.setDoOutput(true);
  19. httpURLConnection.setDoInput(true);
  20. OutputStream outputStream = httpURLConnection.getOutputStream();
  21. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  22. String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
  23. + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  24. bufferedWriter.write(post_data);
  25. bufferedWriter.flush();
  26. bufferedWriter.close();
  27. outputStream.close();
  28. InputStream inputStream = httpURLConnection.getInputStream();
  29. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  30. String result = "";
  31. String line = "";
  32. while ((line = bufferedReader.readLine()) != null) {
  33. result += line;
  34. }
  35. bufferedReader.close();
  36. inputStream.close();
  37. httpURLConnection.disconnect();
  38. return result;
  39. } catch (MalformedURLException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. return null;
  47. }
  48.  
  49. @Override
  50. protected void onPreExecute() {
  51. alertDialog = new AlertDialog.Builder(context).create();
  52. alertDialog.setTitle("Login Status");
  53. }
  54.  
  55. @Override
  56. protected void onPostExecute(String result) {
  57. alertDialog.setMessage(result);
  58. alertDialog.show();
  59. }
  60.  
  61. @Override
  62. protected void onProgressUpdate(Void... values) {
  63. super.onProgressUpdate(values);
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement