暂无描述

SendExperienceRegistration.java 4.5KB

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