暫無描述

SendWithdrawal.java 4.1KB

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