123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package uprrp.tania.networking;
-
- import android.os.AsyncTask;
- import android.util.Log;
-
- import org.json.JSONException;
- import org.json.JSONObject;
-
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.UnsupportedEncodingException;
- import java.io.Writer;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
-
- import javax.net.ssl.HttpsURLConnection;
-
- import uprrp.tania.utils.URLEventListener;
-
- public class SendWithdrawal extends AsyncTask<String, String, String> {
-
- private static final String TAG = "SendWithdrawal";
- private static final String withdrawalURL = "https://tania.uprrp.edu/withdrawal.php";
- private final URLEventListener myCallback;
-
- public SendWithdrawal(URLEventListener callback) {
- this.myCallback = callback;
- }
-
- private boolean validInputs(String... strings) {
-
- if(strings.length != 1) {
- Log.e(TAG, "Invalid string array length!");
- return false;
- }
-
- String deviceToken = strings[0];
-
- if(deviceToken == null) {
- Log.e(TAG, "Encountered null device token!");
- return false;
- }
-
- return true;
-
- }
-
- // TODO: instead of making a normal POST request with
- // the body data=<JSON>, we should just send the JSON itself
- // This is adding unnecessary layers of complexity when decoding AND encoding
- /*
- JSON format:
- {
- "token": String
- }
- */
- @Override
- protected String doInBackground(String... strings) {
-
- // Validation
- if(!validInputs(strings)) {
- Log.e(TAG, "Invalid inputs given!");
- return null;
- }
-
- // Extract variables
- String deviceToken = strings[0];
-
- try {
-
- // Create JSON
- JSONObject withdrawJSON = new JSONObject();
- withdrawJSON.put("token", deviceToken);
- Log.d(TAG, "Prepared JSON: " + withdrawJSON.toString());
-
- // Encode data
- String encodedRequestBody = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(withdrawJSON.toString(), "UTF-8");
-
- // Send POST data request
- URL url = new URL(withdrawalURL);
- HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
- conn.setDoOutput(true);
- Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
- writer.write(encodedRequestBody);
- writer.close();
-
- // Get the server response
- BufferedReader serverReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuilder serverResponse = new StringBuilder();
-
- String line = "";
- while(line != null) {
- serverResponse.append(line);
- line = serverReader.readLine();
- }
-
- String response = serverResponse.toString();
- Log.d(TAG, "The server's response is: " + response);
- if(response.startsWith("Success")) {
- return response;
- } else {
- return null;
- }
-
- } catch (JSONException e){
- Log.e(TAG, "Couldn't construct withdrawJSON!");
- e.printStackTrace();
- return null;
- } catch (UnsupportedEncodingException e) {
- Log.e(TAG, "Couldn't encode withdrawJSON!");
- e.printStackTrace();
- return null;
- } catch (Exception e) {
- Log.e(TAG, "Couldn't communicate with server during withdrawal!");
- e.printStackTrace();
- return null;
- }
-
- }
-
- @Override
- protected void onPostExecute(String response) {
-
- if(this.myCallback == null) {
- Log.e(TAG, "Callback wasn't initialized first!");
- return;
- }
-
- if(response == null) {
- this.myCallback.onFailure(new Exception("Error occurred during transaction!"));
- return;
- }
-
- this.myCallback.onSuccess();
-
- }
- }
|