Keine Beschreibung

SendUserRegistration.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package uprrp.tania.networking;
  2. import android.os.AsyncTask;
  3. import android.util.Log;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonSyntaxException;
  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.URLEncoder;
  12. import javax.net.ssl.HttpsURLConnection;
  13. import uprrp.tania.models.UserModel;
  14. import uprrp.tania.utils.URLEventListener;
  15. public class SendUserRegistration extends AsyncTask <UserModel, Void, String> {
  16. private static final String TAG = "SendUserRegistration";
  17. private static final String sendUserRegistrationURL = "https://tania.uprrp.edu/registrationAnd.php";
  18. private final URLEventListener myCallback;
  19. public SendUserRegistration(URLEventListener callback) {
  20. this.myCallback = callback;
  21. }
  22. private boolean validInputs(UserModel... users) {
  23. if(users.length != 1) {
  24. Log.e(TAG, "Invalid array length!");
  25. return false;
  26. }
  27. for(int i = 0; i < users.length; i++) {
  28. if(users[i] == null) {
  29. Log.e(TAG, "Encountered null parameter on index " + i);
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. // TODO: instead of making a normal POST request with
  36. // the body data=<JSON>, we should just send the JSON itself
  37. // This is adding unnecessary layers of complexity when decoding AND encoding
  38. /*
  39. JSON format:
  40. {
  41. "email": String,
  42. "birthDate": String,
  43. "password": String,
  44. "gender": String,
  45. "hasJob": String,
  46. "token": String
  47. }
  48. */
  49. @Override
  50. protected String doInBackground(UserModel... users) {
  51. // Validation
  52. if(!validInputs(users)) {
  53. Log.e(TAG, "Invalid inputs given!");
  54. return null;
  55. }
  56. // Extract variables
  57. UserModel user = users[0];
  58. try {
  59. // Serialize user
  60. GsonBuilder builder = new GsonBuilder();
  61. // builder.setPrettyPrinting(); // the server doesn't like pretty printing...
  62. String serializedUser = builder.create().toJson(user);
  63. Log.d(TAG,"RegistrationJSON is \n" + serializedUser);
  64. // Encode data
  65. String encodedRequestBody = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(serializedUser, "UTF-8");
  66. // Send POST data request
  67. URL url = new URL(sendUserRegistrationURL);
  68. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  69. conn.setDoOutput(true);
  70. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  71. wr.write(encodedRequestBody);
  72. wr.flush();
  73. // Get the server response
  74. BufferedReader serverReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  75. StringBuilder serverResponse = new StringBuilder();
  76. String line = "";
  77. while(line != null) {
  78. serverResponse.append(line);
  79. line = serverReader.readLine();
  80. }
  81. String response = serverResponse.toString();
  82. // String response = "lkajhkljsdhlakjsdhaf";
  83. Log.d(TAG, "The server's response is: " + response);
  84. if(response.contains("Success")) {
  85. return response;
  86. } else {
  87. return null;
  88. }
  89. } catch (JsonSyntaxException e) {
  90. Log.e(TAG, "Failed to JSONify User!");
  91. e.printStackTrace();
  92. return null;
  93. } catch (UnsupportedEncodingException e) {
  94. Log.e(TAG, "Couldn't encode registrationJSON!");
  95. e.printStackTrace();
  96. return null;
  97. } catch(Exception e) {
  98. Log.e(TAG,"Couldn't communicate with server while sending user registration!" + e.getMessage());
  99. e.printStackTrace();
  100. return null;
  101. }
  102. }
  103. @Override
  104. protected void onPostExecute(String response) {
  105. if(this.myCallback == null) {
  106. Log.e(TAG, "Callback wasn't initialized first!");
  107. return;
  108. }
  109. if(response == null) {
  110. this.myCallback.onFailure(new Exception("Error occurred during transaction!"));
  111. return;
  112. }
  113. this.myCallback.onSuccess();
  114. }
  115. }