No Description

FetchAssessment.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.JSONException;
  7. import org.json.JSONObject;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import javax.net.ssl.HttpsURLConnection;
  15. import uprrp.tania.utils.URLEventListener;
  16. import uprrp.tania.models.AssessmentModel;
  17. import uprrp.tania.models.EmptyAssessmentModel;
  18. public class FetchAssessment extends AsyncTask<String, Void, JSONObject> {
  19. private static final String TAG = "FetchAssessment";
  20. private static final String assessmentBaseURL = "https://tania.uprrp.edu/getSubQ2.php?tk=";
  21. private final URLEventListener myCallBack;
  22. public FetchAssessment(URLEventListener callback) {
  23. this.myCallBack = callback;
  24. }
  25. private boolean validInputs(String... strings) {
  26. if(strings.length != 1) {
  27. Log.e(TAG, "Invalid string array length!");
  28. return false;
  29. }
  30. for(int i = 0; i < strings.length; i++) {
  31. if(strings[i] == null) {
  32. Log.e(TAG, "Encountered null parameter on index " + i);
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. @Override
  39. protected JSONObject 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(assessmentBaseURL + 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, response.substring(0, 10)); // log
  64. if(response.startsWith("Error:NoHa")) {
  65. return new JSONObject("{}");
  66. } else {
  67. return new JSONObject(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(JSONObject obj) {
  85. if(this.myCallBack == null) {
  86. Log.e(TAG, "Callback wasn't initialized first!");
  87. return;
  88. }
  89. if(obj == null) {
  90. this.myCallBack.onFailure(new Exception("An error occurred during FetchAssessment!"));
  91. return;
  92. }
  93. // If there's no survey available, return empty AssessmentModel
  94. if(obj.length() == 0) {
  95. Log.d(TAG, "SENT EMPTY ASSESSMENT MODEL");
  96. AssessmentModel assessment = new EmptyAssessmentModel();
  97. this.myCallBack.onSuccess(assessment);
  98. return;
  99. }
  100. // Decode into AssessmentModel and send
  101. try {
  102. Log.d(TAG, "rawString:" + obj.toString());
  103. GsonBuilder builder = new GsonBuilder();
  104. builder.setPrettyPrinting();
  105. AssessmentModel assessment = builder.create().fromJson(obj.toString(), AssessmentModel.class);
  106. Log.d(TAG,"reconstructedJSON:\n" + builder.create().toJson(assessment));
  107. this.myCallBack.onSuccess(assessment);
  108. } catch (JsonSyntaxException e) {
  109. this.myCallBack.onFailure(new Exception("Something went wrong during data preparation step in FetchAssessment.onPostExecute()!"));
  110. }
  111. }
  112. }