am_dot_com

DDM 2020-11-19

Nov 19th, 2020 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. package com.joythis.android.minimalhttps;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.widget.TextView;
  9.  
  10. import javax.net.ssl.HttpsURLConnection;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. class MyAsyncTask extends AsyncTask
  15. <
  16. String//input data type (URL)
  17. ,
  18. Void //progress data type
  19. ,
  20. String //result data type (remote content)
  21. >
  22. {
  23. String mStrRemoteContent;
  24.  
  25. //CTRL+O Override
  26. /*
  27. automatically called by the Android framework
  28. immediately before starting the execution of the task
  29. coded in doInBackground
  30. */
  31. @Override
  32. protected void onPreExecute() {
  33. super.onPreExecute();
  34. mStrRemoteContent = "";
  35. mTvRemoteContent.setText("Loading...");
  36. }//onPreExecute
  37.  
  38. /*
  39. here is where we should write the code
  40. that will run on its own thread
  41. and asynchronously
  42. */
  43. @Override
  44. protected String doInBackground(
  45. String... strings //variable number of args of String datatype
  46. )
  47. {
  48. String strUrl = strings[0];
  49.  
  50. mStrRemoteContent = "";
  51. /*
  52. mStrRemoteContent = AmHttp.consumeHttpsAtUrl(
  53. strUrl
  54. );
  55. */
  56. mStrRemoteContent = AmHttp.consumeHttpAtUrl(
  57. strUrl
  58. );
  59.  
  60. //raw consumption until this point
  61.  
  62. //return null;
  63. return mStrRemoteContent;
  64. }//doInBackground
  65.  
  66. /*
  67. automatically called when
  68. doInBackground completes
  69. */
  70. @Override
  71. protected void onPostExecute(String s) {
  72. super.onPostExecute(s);
  73.  
  74. /*
  75. the remote content has been loaded
  76. and will now be displayed in the TextView
  77. */
  78.  
  79. /*
  80. opportunity to process the raw data
  81. */
  82. String strProcessingResult = processRawData(s);
  83.  
  84. //mTvRemoteContent.setText(s);
  85. mTvRemoteContent.setText(strProcessingResult);
  86. }//onPostExecute
  87. }//MyAsyncTask
  88.  
  89. private String processRawData(String pStrRawData){
  90. String strProcessingResult = "";
  91.  
  92. String[] aRecordContactsFromRawData =
  93. pStrRawData.split("\n");
  94.  
  95. for (String strContact : aRecordContactsFromRawData){
  96. String[] aParts = strContact.split("\t");
  97. String strContactName = aParts[0];
  98. String strContactAddress = aParts[1];
  99.  
  100. strProcessingResult+=
  101. String.format(
  102. "Name: %s ; Address: %s\n",
  103. strContactName,
  104. strContactAddress
  105. );
  106. }//for
  107.  
  108. return strProcessingResult;
  109. }//processRawData
  110.  
  111. public static final String SOURCE_HTTPS =
  112. "https://arturmarques.com/edu/contacts.TSV";
  113.  
  114. public static final String SOURCE_HTTP =
  115. "http://arturmarques.com/edu/contacts.TSV";
  116.  
  117. Context mContext;
  118. TextView mTvRemoteContent;
  119.  
  120. MyAsyncTask mAsyncTaskThatReadsTheRemoteContent;
  121.  
  122. @Override
  123. protected void onCreate(Bundle savedInstanceState) {
  124. super.onCreate(savedInstanceState);
  125. setContentView(R.layout.activity_main);
  126.  
  127. init();
  128. }//onCreate
  129.  
  130. void approach1(){
  131. /*
  132. we can NOT consume external resources
  133. in the same thread that handles the user interface!
  134. There is the risk that these resources take too long
  135. to become available: during that time the user interface
  136. would be unresponsive (A.N.R.), which is not acceptable.
  137.  
  138. We must do it in a different thread
  139. We must do it asynchronously
  140. */
  141.  
  142. //E/@AmHttp: android.os.NetworkOnMainThreadException
  143. mTvRemoteContent.setText(
  144. AmHttp.consumeHttpsAtUrl(
  145. SOURCE_HTTPS
  146. )
  147. );
  148. }//approach1
  149.  
  150. /*
  151. will consume in its own thread
  152. will consume asynchronously
  153. */
  154. void approach2(){
  155. /*
  156. use MyAsyncTask via mAsyncTaskThatReadsTheRemoteContent
  157. */
  158. mAsyncTaskThatReadsTheRemoteContent.execute(
  159. SOURCE_HTTPS
  160. );
  161. }//approach2
  162.  
  163. void init(){
  164. mContext = this;
  165. mTvRemoteContent = findViewById(R.id.idTvRemoteContent);
  166. mAsyncTaskThatReadsTheRemoteContent =
  167. new MyAsyncTask();
  168.  
  169. //approach1(); //FAIL!
  170.  
  171. approach2(); //OK!
  172. }//init
  173. }
  174.  
  175. //**
  176.  
  177. package com.joythis.android.minimalhttps;
  178.  
  179. import android.util.Log;
  180.  
  181. import java.io.InputStream;
  182. import java.io.InputStreamReader;
  183. import java.net.HttpURLConnection;
  184. import java.net.URL;
  185. import java.nio.charset.StandardCharsets;
  186.  
  187. import javax.net.ssl.HttpsURLConnection;
  188.  
  189. public class AmHttp {
  190. public final static String TAG = "@AmHttp";
  191. public static final int END_OF_CONTENT = -1;
  192.  
  193. public static String consumeHttpsAtUrl(
  194. String pUrl
  195. ){
  196. String strRemoteContent = "";
  197.  
  198. try{
  199. URL url = new URL(pUrl);
  200. HttpsURLConnection httpsCon =
  201. (HttpsURLConnection)
  202. url.openConnection();
  203.  
  204. if(httpsCon!=null){
  205. InputStream is =
  206. httpsCon.getInputStream();
  207.  
  208. //assures the correct consumption of UTF8 symbols in the stream
  209. InputStreamReader isr =
  210. new InputStreamReader(
  211. is,
  212. StandardCharsets.UTF_8
  213. );
  214.  
  215. char c; int iByte;
  216. while((iByte=isr.read())!=END_OF_CONTENT){
  217. c = (char)iByte;
  218. strRemoteContent += c;
  219. }//while
  220.  
  221. isr.close();
  222. is.close();
  223. return strRemoteContent;
  224. }
  225. }//try
  226. catch (Exception e){
  227. String strMsg = e.toString();
  228. Log.e(
  229. TAG//stamp
  230. ,
  231. strMsg
  232. );
  233. }//catch
  234.  
  235. return strRemoteContent;
  236. }//consumeHttpsAtUrl
  237.  
  238. public static String consumeHttpAtUrl(
  239. String pUrl
  240. ){
  241. String strRemoteContent = "";
  242.  
  243. try{
  244. URL url = new URL(pUrl);
  245. HttpURLConnection httpCon =
  246. (HttpURLConnection)
  247. url.openConnection();
  248.  
  249. if(httpCon!=null){
  250. InputStream is = httpCon.getInputStream();
  251.  
  252. //assures the correct consumption of UTF8 symbols in the stream
  253. InputStreamReader isr =
  254. new InputStreamReader(
  255. is,
  256. StandardCharsets.UTF_8
  257. );
  258.  
  259. char c; int iByte;
  260. while((iByte=isr.read())!=END_OF_CONTENT){
  261. c = (char)iByte;
  262. strRemoteContent += c;
  263. }//while
  264.  
  265. isr.close();
  266. is.close();
  267. return strRemoteContent;
  268. }//if
  269. }//try
  270. catch (Exception e){
  271. String strMsg = e.toString();
  272. Log.e(
  273. TAG//stamp
  274. ,
  275. strMsg
  276. );
  277. }//catch
  278.  
  279. return strRemoteContent;
  280. }//consumeHttpAtUrl
  281. }//AmHttp
  282.  
  283. //**
  284.  
  285. <?xml version="1.0" encoding="utf-8"?>
  286. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  287. xmlns:app="http://schemas.android.com/apk/res-auto"
  288. xmlns:tools="http://schemas.android.com/tools"
  289. android:layout_width="match_parent"
  290. android:layout_height="match_parent"
  291. tools:context=".MainActivity">
  292.  
  293. <TextView
  294. android:id="@+id/idTvRemoteContent"
  295. android:layout_width="wrap_content"
  296. android:layout_height="wrap_content"
  297. android:text="Hello World!"
  298. app:layout_constraintBottom_toBottomOf="parent"
  299. app:layout_constraintLeft_toLeftOf="parent"
  300. app:layout_constraintRight_toRightOf="parent"
  301. app:layout_constraintTop_toTopOf="parent" />
  302.  
  303. </androidx.constraintlayout.widget.ConstraintLayout>
  304.  
  305. //**
  306.  
  307. <?xml version="1.0" encoding="utf-8"?>
  308. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  309. package="com.joythis.android.minimalhttps">
  310.  
  311. <!-- non-dangerous permission -->
  312. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  313.  
  314. <application
  315. android:allowBackup="true"
  316. android:icon="@mipmap/ic_launcher"
  317. android:label="@string/app_name"
  318. android:roundIcon="@mipmap/ic_launcher_round"
  319. android:supportsRtl="true"
  320. android:theme="@style/Theme.MinimalHTTPS">
  321. <activity android:name=".MainActivity">
  322. <intent-filter>
  323. <action android:name="android.intent.action.MAIN" />
  324.  
  325. <category android:name="android.intent.category.LAUNCHER" />
  326. </intent-filter>
  327. </activity>
  328. </application>
  329.  
  330. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment