暂无描述

FetchUserStatus.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import javax.net.ssl.HttpsURLConnection;
  16. import uprrp.tania.utils.URLEventListener;
  17. import uprrp.tania.models.EmptyUserStatusModel;
  18. import uprrp.tania.models.UserStatusModel;
  19. public class FetchUserStatus extends AsyncTask<String, Void, JSONArray> {
  20. private static final String TAG = "FetchUserStatus";
  21. private static final String userStatusBaseURL = "https://tania.uprrp.edu/status.php?tk=";
  22. private final URLEventListener myCallBack;
  23. public FetchUserStatus(URLEventListener callback) {
  24. this.myCallBack = callback;
  25. }
  26. private boolean validInputs(String... strings) {
  27. if(strings.length != 1) {
  28. Log.e(TAG, "Invalid string array length!");
  29. return false;
  30. }
  31. String deviceToken = strings[0];
  32. if(deviceToken == null) {
  33. Log.e(TAG, "Encountered null device token!");
  34. return false;
  35. }
  36. return true;
  37. }
  38. @Override
  39. protected JSONArray doInBackground(String... strings) {
  40. // Validation
  41. if(!validInputs(strings)) {
  42. Log.e(TAG, "Invalid inputs given!");
  43. return null;
  44. }
  45. // Extract variables
  46. String deviceToken = strings[0];
  47. try {
  48. // Send GET data request
  49. URL url = new URL(userStatusBaseURL + deviceToken);
  50. Log.d(TAG, "token:" + deviceToken); // log
  51. Log.d(TAG, "url:" + url.toString()); // log
  52. HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  53. InputStream inputStream = conn.getInputStream();
  54. // Get the server response
  55. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  56. StringBuilder serverResponse = new StringBuilder();
  57. String line = "";
  58. while(line != null) {
  59. serverResponse.append(line);
  60. line = bufferedReader.readLine();
  61. }
  62. String response = serverResponse.toString();
  63. Log.d(TAG, "rawString:" + response); // log
  64. if(response.contains("No joda")) { // no esta inscrito
  65. return new JSONArray("[]");
  66. } else {
  67. return new JSONArray(response);
  68. }
  69. } catch (MalformedURLException e) {
  70. Log.e(TAG, "Wrong URL used!");
  71. e.printStackTrace();
  72. return null;
  73. } catch (IOException e) {
  74. Log.e(TAG, "IOStream error occurred!");
  75. e.printStackTrace();
  76. return null;
  77. } catch (JSONException e) {
  78. Log.e(TAG, "Couldn't construct return JSON!");
  79. e.printStackTrace();
  80. return null;
  81. }
  82. }
  83. @Override
  84. protected void onPostExecute(JSONArray arr) {
  85. super.onPostExecute(arr);
  86. if(this.myCallBack == null) {
  87. Log.e(TAG, "Callback wasn't initialized first!");
  88. return;
  89. }
  90. if(arr == null) {
  91. this.myCallBack.onFailure(new Exception("Something went wrong with FetchUserStatus.doInBackground()!"));
  92. return;
  93. }
  94. // If json array is empty, return empty user status
  95. if(arr.length() == 0) {
  96. Log.d(TAG, "SENT EMPTY USER STATUS");
  97. UserStatusModel userData = new EmptyUserStatusModel();
  98. this.myCallBack.onSuccess(userData);
  99. return;
  100. }
  101. // Decode into UserStatusModel and send
  102. try {
  103. // TODO: Should find moment that is nearest to current date,
  104. // instead of assuming it will always be sorted (just in case)
  105. JSONObject obj = arr.getJSONObject(0);
  106. // Actual decoding
  107. Log.d(TAG, "rawString:" + obj.toString());
  108. GsonBuilder builder = new GsonBuilder();
  109. builder.setPrettyPrinting();
  110. UserStatusModel userStatus = builder.create().fromJson(obj.toString(), UserStatusModel.class);
  111. Log.d(TAG,"reconstructedJSON:\n" + builder.create().toJson(userStatus));
  112. this.myCallBack.onSuccess(userStatus);
  113. } catch(JsonSyntaxException e) {
  114. this.myCallBack.onFailure(new Exception("Something went wrong during Gson decoding step in FetchUserStatus.onPostExecute()!"));
  115. } catch (JSONException e) {
  116. this.myCallBack.onFailure(new Exception("Something went wrong during JSONObject extraction step in FetchUserStatus.onPostExecute()!"));
  117. }
  118. }
  119. }