No Description

FetchAssessment.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package uprrp.tania.networking;
  2. import android.content.res.Resources;
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5. import com.google.gson.GsonBuilder;
  6. import com.google.gson.JsonSyntaxException;
  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.R;
  17. import uprrp.tania.URLEventListener;
  18. import uprrp.tania.models.AssessmentModel;
  19. import uprrp.tania.models.EmptyAssessmentModel;
  20. public class FetchAssessment extends AsyncTask<String, Void, JSONObject> {
  21. private static final String TAG = "FetchAssessment";
  22. private final URLEventListener myCallBack;
  23. public FetchAssessment(URLEventListener callback) {
  24. this.myCallBack = callback;
  25. }
  26. @Override
  27. protected JSONObject doInBackground(String... deviceTokenArray) {
  28. String deviceToken = deviceTokenArray[0]; // array will only ever contain a single element
  29. try {
  30. String getMomentsBaseURL = Resources.getSystem().getString(R.string.assessmentsBaseURL);
  31. URL url = new URL(getMomentsBaseURL + deviceToken);
  32. Log.d(TAG, "token:" + deviceToken); // log
  33. Log.d(TAG, "url:" + url.toString()); // log
  34. HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
  35. InputStream inputStream = httpsURLConnection.getInputStream();
  36. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  37. StringBuilder serverResponse = new StringBuilder();
  38. String line = "";
  39. while (line != null) {
  40. serverResponse.append(line);
  41. line = bufferedReader.readLine();
  42. }
  43. String response = serverResponse.toString();
  44. Log.d(TAG, response.substring(0, 10)); // log
  45. if(response.startsWith("Error:NoHa")) {
  46. return new JSONObject("{}");
  47. } else {
  48. return new JSONObject(response);
  49. }
  50. } catch (MalformedURLException e) {
  51. Log.e(TAG, "Wrong URL used!");
  52. e.printStackTrace();
  53. return null;
  54. } catch (IOException e) {
  55. Log.e(TAG, "IOStream error occurred!");
  56. e.printStackTrace();
  57. return null;
  58. } catch (JSONException e) {
  59. Log.e(TAG, "Couldn't construct return JSON!");
  60. e.printStackTrace();
  61. return null;
  62. }
  63. }
  64. @Override
  65. protected void onPostExecute(JSONObject obj) {
  66. if(this.myCallBack == null) {
  67. this.myCallBack.onFailure(new Exception("Callback wasn't initialized first!"));
  68. return;
  69. }
  70. if(obj == null) {
  71. this.myCallBack.onFailure(new Exception("An error occurred during FetchAssessment!"));
  72. return;
  73. }
  74. // If there's no survey available, return empty AssessmentModel
  75. if(obj.length() == 0) {
  76. Log.d(TAG, "SENT EMPTY ASSESSMENT MODEL");
  77. AssessmentModel assessment = new EmptyAssessmentModel();
  78. this.myCallBack.onSuccess(assessment);
  79. return;
  80. }
  81. // Decode into AssessmentModel and send
  82. try {
  83. Log.d(TAG, "rawString:" + obj.toString());
  84. GsonBuilder builder = new GsonBuilder();
  85. builder.setPrettyPrinting();
  86. AssessmentModel assessment = builder.create().fromJson(obj.toString(), AssessmentModel.class);
  87. Log.d(TAG,"reconstructedJSON:\n" + builder.create().toJson(assessment));
  88. this.myCallBack.onSuccess(assessment);
  89. } catch (JsonSyntaxException e) {
  90. this.myCallBack.onFailure(new Exception("Something went wrong during data preparation step in FetchAssessment.onPostExecute()!"));
  91. }
  92. }
  93. }