Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. Context context;
  2. AlertDialog alertDialog;
  3. boolean correct;
  4. public BackgrroundWorker(Context context){
  5. this.context = context;
  6. }
  7. @Override
  8. protected String doInBackground(String... params) {
  9. String type = params[0];
  10. String login_url = "random";
  11. String register_url = "random";
  12. if(type.equals("login")){
  13. try {
  14. String username = params[1];
  15. String password = params[2];
  16. URL url = new URL(login_url);
  17. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  18. httpURLConnection.setRequestMethod("POST");
  19. httpURLConnection.setDoOutput(true);
  20. httpURLConnection.setDoInput(true);
  21. OutputStream outputStream = httpURLConnection.getOutputStream();
  22. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  23. String post_data = URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(username,"UTF-8")+ "&"
  24. +URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password,"UTF-8");
  25. bufferedWriter.write(post_data);
  26. bufferedWriter.flush();
  27. bufferedWriter.close();
  28. outputStream.close();
  29. InputStream inputStream = httpURLConnection.getInputStream();
  30. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  31. String result = "";
  32. String line = "";
  33. correct = false;
  34. while((line = bufferedReader.readLine()) != null){
  35. result += line;
  36. }
  37. bufferedReader.close();
  38. inputStream.close();
  39. httpURLConnection.disconnect();
  40. System.out.println(result);
  41. if (result.equalsIgnoreCase("login success"))
  42. correct=true;
  43. return result;
  44. } catch (MalformedURLException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49.  
  50. }
  51. return null;
  52. }
  53.  
  54. @Override
  55. protected void onPreExecute() {
  56. progressDialog = ProgressDialog.show(context, "Login",
  57. "Please wait for a while.", true);
  58. alertDialog = new AlertDialog.Builder(context).create();
  59. alertDialog.setTitle("Login status");
  60.  
  61.  
  62.  
  63. }
  64.  
  65. @Override
  66. protected void onPostExecute(String result) {
  67. if(!result.equals("login success")) {
  68. alertDialog.setMessage(result);
  69. alertDialog.show();
  70.  
  71. }
  72. progressDialog.dismiss();
  73. }
  74.  
  75. @Override
  76. protected void onProgressUpdate(Void... values) {
  77. super.onProgressUpdate(values);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement