Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 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 login_url = "https://prontocars.000webhostapp.com/UserRegistration/login.php";
  11. if(type.equals("login")) {
  12. try {
  13. String username = params[1];
  14. String password = params[2];
  15. URL url = new URL(login_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. return null;
  46. }
  47.  
  48. @Override
  49. protected void onPreExecute() {
  50. alertDialog = new AlertDialog.Builder(context).create();
  51. alertDialog.setTitle("Login Status");
  52. }
  53.  
  54. @Override
  55. protected void onPostExecute(String result) {
  56. // alertDialog.setMessage(result);
  57. // alertDialog.show();
  58. if (result.contentEquals("success")) {
  59. Intent ltm = new Intent(context, MainActivity.class);
  60. context.startActivity(ltm);
  61.  
  62. } else {
  63. Toast toast = Toast.makeText(context, "Nombre de usuario o contraseña incorrecta", Toast.LENGTH_SHORT);
  64. toast.show();
  65. }
  66. }
  67.  
  68. @Override
  69. protected void onProgressUpdate (Void...values){
  70. super.onProgressUpdate(values);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement