am_dot_com

PDM2 20201117

Nov 17th, 2020 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. package com.joythis.android.ex20201117;
  2.  
  3. import android.util.Log;
  4.  
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.nio.charset.StandardCharsets;
  10.  
  11. import javax.net.ssl.HttpsURLConnection;
  12.  
  13. public class AmHttpIO {
  14. public final static int END_OF_CONTENTS = -1;
  15.  
  16. String mContentsRead = "";
  17.  
  18. public String consumeHttpsUrl(
  19. String pUrl
  20. ){
  21. int iBytesRead = 0;
  22. String strContents = "";
  23.  
  24. try {
  25. URL url = new URL(pUrl);
  26. HttpsURLConnection httpsCon =
  27. (HttpsURLConnection)url.openConnection();
  28.  
  29. InputStream is = httpsCon.getInputStream();
  30.  
  31. if (is!=null){
  32. InputStreamReader isr = new InputStreamReader(
  33. is,
  34. StandardCharsets.UTF_8
  35. );
  36. if (isr!=null){
  37. char c; int iByte;
  38.  
  39. while((iByte=isr.read())!=END_OF_CONTENTS){
  40. c = (char)iByte;
  41. strContents+=c;
  42. }//while
  43. isr.close();
  44. is.close();
  45. iBytesRead = strContents.length();
  46.  
  47. //mContentsRead = strContents;
  48. //return iBytesRead;
  49. return strContents;
  50. }//if the InputStreamReader object is non null
  51. else{
  52. throw new Exception(
  53. "null InputStreamReader!"
  54. );
  55. }
  56. }//if the InputStream object is non null
  57. else{
  58. throw new Exception(
  59. "null InputStream!"
  60. );
  61. }//else
  62. }//try
  63. catch (Exception e){
  64. Log.e(
  65. this.getClass().getName(),
  66. e.toString()
  67. );
  68. }//catch
  69.  
  70. //return iBytesRead;
  71. return strContents;
  72. }//consumeHttpsUrl
  73.  
  74. public String consumeHttpUrl(
  75. String pUrl
  76. ){
  77. int iBytesRead = 0;
  78. String strContents = "";
  79.  
  80. try {
  81. URL url = new URL(pUrl);
  82. HttpURLConnection httpsCon =
  83. (HttpURLConnection)url.openConnection();
  84.  
  85. InputStream is = httpsCon.getInputStream();
  86.  
  87. if (is!=null){
  88. InputStreamReader isr = new InputStreamReader(
  89. is,
  90. StandardCharsets.UTF_8
  91. );
  92. if (isr!=null){
  93. char c; int iByte;
  94.  
  95. while((iByte=isr.read())!=END_OF_CONTENTS){
  96. c = (char)iByte;
  97. strContents+=c;
  98. }//while
  99. isr.close();
  100. is.close();
  101. iBytesRead = strContents.length();
  102.  
  103. //mContentsRead = strContents;
  104. //return iBytesRead;
  105. return strContents;
  106. }//if the InputStreamReader object is non null
  107. else{
  108. throw new Exception(
  109. "null InputStreamReader!"
  110. );
  111. }
  112. }//if the InputStream object is non null
  113. else{
  114. throw new Exception(
  115. "null InputStream!"
  116. );
  117. }//else
  118. }//try
  119. catch (Exception e){
  120. Log.e(
  121. this.getClass().getName(),
  122. e.toString()
  123. );
  124. }//catch
  125.  
  126. //return iBytesRead;
  127. return strContents;
  128. }//consumeHttpUrl
  129.  
  130. public String getLastContentRead(){
  131. return this.mContentsRead;
  132. }
  133. }//AmHttpIO
  134.  
  135.  
  136. //**
  137.  
  138. package com.joythis.android.ex20201117;
  139.  
  140. import androidx.appcompat.app.AppCompatActivity;
  141.  
  142. import android.os.AsyncTask;
  143. import android.os.Bundle;
  144. import android.widget.TextView;
  145. import android.widget.Toast;
  146.  
  147. public class DisplayHttpContentActivity extends AppCompatActivity {
  148.  
  149. class LoadAndDisplayAsynchronously
  150. extends AsyncTask
  151. <
  152. String, //URL / data type of the input data (compatible with doinbackground)
  153. Void, //data type for the progress indicator (Void if none)
  154. String //contents read / data type for the result of entire background execution
  155. >
  156. {
  157. /*
  158. automatically called by the framework just before it calls "doinbackground"
  159. */
  160. @Override
  161. protected void onPreExecute() {
  162. super.onPreExecute();
  163.  
  164. mTvFeedback.setText("Started loading...");
  165. }//onPreExecute
  166.  
  167. @Override
  168. protected String doInBackground(String... strings) {
  169. String strUrl = strings[0];
  170.  
  171. String strContentRead = mHttpIO.consumeHttpsUrl(strUrl);
  172. //return null;
  173. return strContentRead;
  174. }//doInBackground
  175.  
  176. /*
  177. will be automatically called by the framework when "doinbackground" ends
  178. */
  179. @Override
  180. protected void onPostExecute(String s) {
  181. super.onPostExecute(s);
  182.  
  183. //process the data!
  184. //extracting and parsing
  185. //extraction
  186. String[] aRecords = s.split("\n");
  187.  
  188. //parsing
  189. String strProcessed = "";
  190. for (String strRecord : aRecords){
  191. String[] aParts = strRecord.split("\t");
  192. String strDate = aParts[0];
  193. String strDescription = aParts[1];
  194.  
  195. strProcessed+="Date: "+strDate+"[ "+ strDescription + " ]\n";
  196. }//for
  197.  
  198. //mTvFeedback.setText(s);
  199. mTvFeedback.setText(strProcessed);
  200. }//onPostExecute
  201. }//LoadAndDisplayAsynchronously
  202.  
  203. TextView mTvFeedback;
  204.  
  205. AmHttpIO mHttpIO;
  206.  
  207. @Override
  208. protected void onCreate(Bundle savedInstanceState) {
  209. super.onCreate(savedInstanceState);
  210. setContentView(R.layout.activity_main);
  211.  
  212. init();
  213. }//onCreate
  214.  
  215. public final static String TEST_URL =
  216. "https://arturmarques.com/edu/pdm2/dates.TSV";
  217.  
  218. void init(){
  219. mHttpIO = new AmHttpIO();
  220.  
  221. mTvFeedback = findViewById(R.id.idTvFeedback);
  222.  
  223. /*
  224. valid algorithm that Android refuses to execute
  225. because
  226. "NetworkOnMainThreadException"
  227. solution: execute it asynchronously
  228. */
  229.  
  230. try {
  231. loadAndDisplay(
  232. TEST_URL,
  233. mTvFeedback
  234. );
  235. }//try
  236. catch(Exception e){
  237. Toast t = Toast.makeText(
  238. this,
  239. e.toString(),
  240. Toast.LENGTH_LONG
  241. );
  242. t.show();
  243. }//catch
  244.  
  245. loadAndDisplay2();
  246. }//init
  247.  
  248. void loadAndDisplay(
  249. String pUrl,
  250. TextView pTvWhereToDisplayTheLoadedContents
  251. ){
  252. /*
  253. mHttpIO.consumeHttpsUrl(pUrl);
  254. String strContents = mHttpIO.getLastContentRead();
  255. */
  256. String strContents = mHttpIO.consumeHttpUrl(pUrl);
  257. //String strContents = mHttpIO.consumeHttpsUrl(pUrl);
  258. pTvWhereToDisplayTheLoadedContents.setText(strContents);
  259. }//loadAndDisplay
  260.  
  261. void loadAndDisplay2(){
  262. LoadAndDisplayAsynchronously taskToConsumeHttpsAsynchronously =
  263. new LoadAndDisplayAsynchronously();
  264.  
  265. taskToConsumeHttpsAsynchronously.execute(TEST_URL);
  266. }//loadAndDisplay2
  267.  
  268. }//DisplayHttpContentActivity
Advertisement
Add Comment
Please, Sign In to add comment