No Description

SendAccountRecovery.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package uprrp.tania.networking;
  2. import android.os.AsyncTask;
  3. import android.util.Log;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.net.URLEncoder;
  13. import uprrp.tania.utils.URLEventListener;
  14. public class SendAccountRecovery extends AsyncTask<String, String, String> {
  15. private static final String TAG = "SendAccountRecovery";
  16. private static final String loginBaseURL = "https://tania.uprrp.edu/recoverAccount.php";
  17. private final URLEventListener myCallback;
  18. public SendAccountRecovery(URLEventListener callback) {
  19. this.myCallback = callback;
  20. }
  21. private boolean validInputs(String... strings) {
  22. if(strings.length != 3) {
  23. Log.e(TAG, "Invalid string array length!");
  24. return false;
  25. }
  26. String deviceToken = strings[0];
  27. String email = strings[1];
  28. String password = strings[2];
  29. if(email == null) {
  30. Log.e(TAG, "Encountered null email!");
  31. return false;
  32. } else if (deviceToken == null) {
  33. Log.e(TAG, "Encountered null device token!");
  34. return false;
  35. } else if (password == null) {
  36. Log.e(TAG, "Encountered null password!");
  37. return false;
  38. }
  39. return true;
  40. }
  41. // TODO: instead of making a normal POST request with
  42. // the body data=<JSON>, we should just send the JSON itself
  43. // This is adding unnecessary layers of complexity when decoding AND encoding
  44. /*
  45. JSON format:
  46. {
  47. "email": String,
  48. "password": String,
  49. "token": String,
  50. }
  51. */
  52. @Override
  53. protected String doInBackground(String... strings) {
  54. // Validation
  55. if(!validInputs(strings)) {
  56. Log.e(TAG, "Invalid inputs given!");
  57. return null;
  58. }
  59. // Extract variables
  60. String deviceToken = strings[0];
  61. String email = strings[1];
  62. String password = strings[2];
  63. try {
  64. // Create JSON
  65. JSONObject accountRecoveryJSON = new JSONObject();
  66. accountRecoveryJSON.put("email", email);
  67. accountRecoveryJSON.put("password", password);
  68. accountRecoveryJSON.put("token", deviceToken);
  69. Log.d(TAG, "RecoveryJSON: " + accountRecoveryJSON.toString());
  70. // Encode data
  71. String encodedRequestBody = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(accountRecoveryJSON.toString(), "UTF-8");
  72. // Send POST data request
  73. URL url = new URL(loginBaseURL);
  74. URLConnection conn = url.openConnection();
  75. conn.setDoOutput(true);
  76. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  77. wr.write(encodedRequestBody);
  78. wr.flush();
  79. // Get the server response
  80. BufferedReader serverReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  81. StringBuilder serverResponse = new StringBuilder();
  82. String line = "";
  83. while(line != null) {
  84. serverResponse.append(line);
  85. line = serverReader.readLine();
  86. }
  87. String response = serverResponse.toString();
  88. Log.d(TAG, "The server's response is: " + response);
  89. if(response.contains("Error")) {
  90. return null;
  91. } else {
  92. return response;
  93. }
  94. } catch (JSONException e) {
  95. Log.e(TAG, "Couldn't prepare accountRecoveryJSON!");
  96. e.printStackTrace();
  97. return null;
  98. } catch (UnsupportedEncodingException e) {
  99. Log.e(TAG, "Couldn't encode accountRecoveryJSON!");
  100. e.printStackTrace();
  101. return null;
  102. } catch(Exception e) {
  103. Log.e(TAG, "Couldn't communicate with server while recovering account!");
  104. e.printStackTrace();
  105. return null;
  106. }
  107. }
  108. @Override
  109. protected void onPostExecute(String response) {
  110. if(this.myCallback == null) {
  111. Log.e(TAG, "Callback wasn't initialized first!");
  112. return;
  113. }
  114. if(response == null) {
  115. this.myCallback.onFailure(new Exception("Error occurred during transaction!"));
  116. return;
  117. }
  118. this.myCallback.onSuccess(response);
  119. }
  120. }