am_dot_com

DDM 2022-12-20

Dec 20th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. implementation 'com.squareup.okhttp3:okhttp:4.9.0'
  2.  
  3. package com.joythis.android.simplesthttprw;
  4.  
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.util.Log;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15.  
  16. import javax.net.ssl.HttpsURLConnection;
  17.  
  18. import okhttp3.FormBody;
  19. import okhttp3.OkHttpClient;
  20. import okhttp3.Request;
  21. import okhttp3.RequestBody;
  22. import okhttp3.Response;
  23.  
  24. /**
  25. * Created by Artur Marques on 2018.
  26. */
  27.  
  28. public class AmIoHttp {
  29.  
  30. public static String io_https_ReadAll(
  31. String pUrl
  32. ){
  33. String ret="";
  34. HttpURLConnection urlConnection = null;
  35. try {
  36. URL url = new URL(pUrl);
  37.  
  38. urlConnection = (HttpsURLConnection) url.openConnection();
  39. InputStream in = urlConnection.getInputStream();
  40. InputStreamReader isr = new InputStreamReader(in);
  41. BufferedReader br = new BufferedReader(isr);
  42.  
  43. String strLine = "";
  44. while ((strLine = br.readLine())!=null){
  45. ret+=strLine;
  46. }
  47.  
  48. br.close();
  49. isr.close();
  50. in.close();
  51. } catch (Exception e) {
  52. Log.e(
  53. "@AmIoHttp",
  54. e.toString()
  55. );
  56. e.printStackTrace();
  57. } finally //The finally block always executes when the try block exits
  58. {
  59. if (urlConnection != null) {
  60. urlConnection.disconnect();
  61. }//if
  62. }//finally
  63. return ret;
  64. }//io_https_ReadAll
  65.  
  66. public static String io_http_ReadAll(
  67. String pUrl
  68. ){
  69. String ret="";
  70. HttpURLConnection urlConnection = null;
  71. try {
  72. URL url = new URL(pUrl);
  73.  
  74. urlConnection = (HttpURLConnection) url.openConnection();
  75. InputStream in = urlConnection.getInputStream();
  76. InputStreamReader isr = new InputStreamReader(in);
  77. BufferedReader br = new BufferedReader(isr);
  78.  
  79. String strLine = "";
  80. while ((strLine = br.readLine())!=null){
  81. ret+=strLine;
  82. }
  83.  
  84. br.close();
  85. isr.close();
  86. in.close();
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally //The finally block always executes when the try block exits
  90. {
  91. if (urlConnection != null) {
  92. urlConnection.disconnect();
  93. }//if
  94. }//finally
  95. return ret;
  96. }//io_http_ReadAll
  97.  
  98.  
  99. public final static String TAG = "@AmIoHttp";
  100. public static Bitmap readBitmapFromUrl(
  101. String pUrl
  102. ){
  103. Bitmap ret = null;
  104.  
  105. try{
  106. URL url = new URL(pUrl);
  107. InputStream is = url.openStream();
  108. ret = BitmapFactory.decodeStream(is);
  109. is.close();
  110. }
  111. catch (Exception e){
  112. Log.e(
  113. TAG,
  114. e.getMessage()
  115. );
  116. }
  117.  
  118. return ret;
  119. }//readBitmapFromUrl
  120.  
  121. public static String postShare(
  122. String pUrl,
  123. String pWhen,
  124. String pWhat
  125. ){
  126. OkHttpClient client = new OkHttpClient();
  127.  
  128. //build a post request
  129. RequestBody requestBody = new FormBody.Builder()
  130. .add("when", pWhen)
  131. .add("what", pWhat)
  132. .build();
  133.  
  134. Request request = new Request.Builder()
  135. //.url("https://arturmarques.com/edu/ddm/files/w12/receiver.php")
  136. .url(pUrl)
  137. .post(requestBody)
  138. .build();
  139.  
  140. try {
  141. Response response = client.newCall(request).execute();
  142.  
  143. // Do something with the response.
  144. String strRet = response.body().string();
  145. return strRet;
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149.  
  150. return "FAIL";
  151. }//postShare
  152. }//AmIoHttp
  153.  
Advertisement
Add Comment
Please, Sign In to add comment