Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. @Override
  2. protected String doInBackground(String... params) {
  3. String reg_url = "http://10.0.2.2/app/register.php";
  4. String login_url = "http://10.0.2.2/app/login.php";
  5. String method = params[0];
  6. if (method.equals("register")) {
  7. String username = params[1]; //parameters for register
  8. String password = params[2];
  9. String phone_no = params[3];
  10. try {
  11. //connection to mysql database
  12. URL url = new URL(reg_url);
  13. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  14. httpURLConnection.setRequestMethod("POST");
  15. httpURLConnection.setDoOutput(true);
  16. httpURLConnection.setDoInput(true);
  17. OutputStream OS = httpURLConnection.getOutputStream();
  18. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  19. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
  20. URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
  21. URLEncoder.encode("phone_no", "UTF-8") + "=" + URLEncoder.encode(phone_no, "UTF-8");
  22. bufferedWriter.write(data);
  23. bufferedWriter.flush();
  24. bufferedWriter.close();
  25. OS.close();
  26. InputStream IS = httpURLConnection.getInputStream();
  27. IS.close();
  28. //httpURLConnection.connect();
  29. httpURLConnection.disconnect();
  30. return "Registration Successful";
  31. } catch (MalformedURLException e) {
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }else if(method.equals("login")) //login class
  37. {
  38. String id = params[1];
  39. String password = params[2];
  40.  
  41. try {
  42. URL url = new URL(login_url);
  43. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  44. httpURLConnection.setRequestMethod("POST");
  45. httpURLConnection.setDoOutput(true);
  46. httpURLConnection.setDoInput(true);
  47. OutputStream outputStream = httpURLConnection.getOutputStream();
  48. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  49. String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
  50. URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
  51. bufferedWriter.write(data);
  52. bufferedWriter.flush();
  53. bufferedWriter.close();
  54. outputStream.close();
  55. InputStream inputStream = httpURLConnection.getInputStream();
  56. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
  57. String response = "";
  58. String line = "";
  59. while ((line = bufferedReader.readLine())!=null)
  60. {
  61. response+= line;
  62. }
  63. bufferedReader.close();
  64. inputStream.close();
  65. httpURLConnection.disconnect();
  66. return response;
  67. } catch (MalformedURLException e) {
  68. e.printStackTrace();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return null;
  74. }
  75. @Override
  76. protected void onProgressUpdate(Void... values) {
  77. super.onProgressUpdate(values);
  78. }
  79.  
  80. @Override
  81. protected void onPostExecute(String result) {
  82. super.onPostExecute(result);
  83. if(result.equals("Registration Successful"))
  84. {
  85. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  86. }
  87. else if(result.equals("login successful"))
  88. //if login is successful, it sends the user to another activity
  89. {
  90. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  91. ctx.startActivity(new Intent(ctx, sync.class));
  92. }
  93. else{
  94. Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement