Guest User

Untitled

a guest
Dec 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. public class TestUrlConection {
  2.  
  3. private static String responseServer = "";
  4. private static String method = null;
  5. private static ProgressDialog progressDialog = null;
  6. private static String getFireBase = null;
  7. private static String urlAddress;
  8.  
  9. private static Context activityContext;
  10. public static OnAsyncRequestComplete caller;
  11.  
  12. public interface OnAsyncRequestComplete {
  13. void asyncResponse(String response);
  14. }// Fin OnAsyncRequestComplete
  15.  
  16. public static void request(Context activity, String url, String requestMethod ){
  17.  
  18. activityContext = activity;
  19. urlAddress = url;
  20. method = requestMethod;
  21. caller = ( OnAsyncRequestComplete )activityContext;
  22. new MakeRequest().execute();
  23.  
  24. }// Fin request
  25.  
  26. public static class MakeRequest extends AsyncTask<String, String, String>{
  27.  
  28.  
  29. @Override
  30. protected void onPreExecute() {
  31. super.onPreExecute();
  32. progressDialog = new ProgressDialog(activityContext);
  33. }
  34.  
  35. @Override
  36. protected void onPostExecute(String response) {
  37. super.onPostExecute(response);
  38. if (progressDialog != null && progressDialog.isShowing()) {
  39. progressDialog.dismiss();
  40. }
  41. caller.asyncResponse( response );
  42.  
  43. }// Fin onPostExecute
  44.  
  45. @Override
  46. protected void onProgressUpdate(String... values) {
  47. super.onProgressUpdate( values );
  48. }
  49.  
  50. @Override
  51. protected void onCancelled(String s) {
  52. super.onCancelled(s);
  53. }
  54.  
  55. @Override
  56. protected void onCancelled() {
  57. super.onCancelled();
  58. }
  59.  
  60. @Override
  61. protected String doInBackground(String... strings) {
  62.  
  63. if (method == "POST") {
  64.  
  65. }
  66. if (method == "GET") {
  67. try {
  68. responseServer = get();
  69. } catch (InterruptedException e) {
  70. e.printStackTrace();
  71. } catch (ExecutionException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. if (method == "GETTOKEN") {
  76. return firebaseinstanceIdTask();
  77. }
  78.  
  79. return null;
  80. }// Fin doInBackground
  81.  
  82. }// Fin MakeRequest
  83.  
  84. private String get() {
  85.  
  86. try {
  87.  
  88. URL urlServer = new URL( urlAddress );
  89. HttpURLConnection conectionServer = (HttpURLConnection) urlServer.openConnection();
  90. conectionServer.setRequestMethod(method);
  91. conectionServer.setRequestProperty("content-type", "application/json");
  92. conectionServer.connect();
  93. InputStream inputStream = conectionServer.getInputStream();
  94. StringBuffer stringBuffer = new StringBuffer();
  95.  
  96. if (inputStream == null) {
  97. return null;
  98. }
  99.  
  100. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  101. String line;
  102. while ((line = bufferedReader.readLine()) != null) {
  103. stringBuffer.append(line + "n");
  104. }
  105.  
  106. if (stringBuffer.length() == 0) {
  107. return null;
  108. }
  109.  
  110. String forecastJsonStr = stringBuffer.toString();
  111. return forecastJsonStr;
  112.  
  113. } catch (MalformedURLException e) {
  114.  
  115. e.printStackTrace();
  116.  
  117. } catch (IOException e) {
  118.  
  119. e.printStackTrace();
  120. }
  121. return null;
  122. }// fin get
  123.  
  124.  
  125. private static String firebaseinstanceIdTask() {
  126.  
  127. FireBaseIdService instanceIDService = new FireBaseIdService();
  128. instanceIDService.onTokenRefresh();
  129. getFireBase = instanceIDService.getFireBaseToken();
  130. return getFireBase;
  131.  
  132. }// Fin firebaseinstanceIdTask
  133.  
  134. caller = ( OnAsyncRequestComplete )activityContext;
Add Comment
Please, Sign In to add comment