Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.ex20201117;
- import android.util.Log;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import javax.net.ssl.HttpsURLConnection;
- public class AmHttpIO {
- public final static int END_OF_CONTENTS = -1;
- String mContentsRead = "";
- public String consumeHttpsUrl(
- String pUrl
- ){
- int iBytesRead = 0;
- String strContents = "";
- try {
- URL url = new URL(pUrl);
- HttpsURLConnection httpsCon =
- (HttpsURLConnection)url.openConnection();
- InputStream is = httpsCon.getInputStream();
- if (is!=null){
- InputStreamReader isr = new InputStreamReader(
- is,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- char c; int iByte;
- while((iByte=isr.read())!=END_OF_CONTENTS){
- c = (char)iByte;
- strContents+=c;
- }//while
- isr.close();
- is.close();
- iBytesRead = strContents.length();
- //mContentsRead = strContents;
- //return iBytesRead;
- return strContents;
- }//if the InputStreamReader object is non null
- else{
- throw new Exception(
- "null InputStreamReader!"
- );
- }
- }//if the InputStream object is non null
- else{
- throw new Exception(
- "null InputStream!"
- );
- }//else
- }//try
- catch (Exception e){
- Log.e(
- this.getClass().getName(),
- e.toString()
- );
- }//catch
- //return iBytesRead;
- return strContents;
- }//consumeHttpsUrl
- public String consumeHttpUrl(
- String pUrl
- ){
- int iBytesRead = 0;
- String strContents = "";
- try {
- URL url = new URL(pUrl);
- HttpURLConnection httpsCon =
- (HttpURLConnection)url.openConnection();
- InputStream is = httpsCon.getInputStream();
- if (is!=null){
- InputStreamReader isr = new InputStreamReader(
- is,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- char c; int iByte;
- while((iByte=isr.read())!=END_OF_CONTENTS){
- c = (char)iByte;
- strContents+=c;
- }//while
- isr.close();
- is.close();
- iBytesRead = strContents.length();
- //mContentsRead = strContents;
- //return iBytesRead;
- return strContents;
- }//if the InputStreamReader object is non null
- else{
- throw new Exception(
- "null InputStreamReader!"
- );
- }
- }//if the InputStream object is non null
- else{
- throw new Exception(
- "null InputStream!"
- );
- }//else
- }//try
- catch (Exception e){
- Log.e(
- this.getClass().getName(),
- e.toString()
- );
- }//catch
- //return iBytesRead;
- return strContents;
- }//consumeHttpUrl
- public String getLastContentRead(){
- return this.mContentsRead;
- }
- }//AmHttpIO
- //**
- package com.joythis.android.ex20201117;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.Toast;
- public class DisplayHttpContentActivity extends AppCompatActivity {
- class LoadAndDisplayAsynchronously
- extends AsyncTask
- <
- String, //URL / data type of the input data (compatible with doinbackground)
- Void, //data type for the progress indicator (Void if none)
- String //contents read / data type for the result of entire background execution
- >
- {
- /*
- automatically called by the framework just before it calls "doinbackground"
- */
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- mTvFeedback.setText("Started loading...");
- }//onPreExecute
- @Override
- protected String doInBackground(String... strings) {
- String strUrl = strings[0];
- String strContentRead = mHttpIO.consumeHttpsUrl(strUrl);
- //return null;
- return strContentRead;
- }//doInBackground
- /*
- will be automatically called by the framework when "doinbackground" ends
- */
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- //process the data!
- //extracting and parsing
- //extraction
- String[] aRecords = s.split("\n");
- //parsing
- String strProcessed = "";
- for (String strRecord : aRecords){
- String[] aParts = strRecord.split("\t");
- String strDate = aParts[0];
- String strDescription = aParts[1];
- strProcessed+="Date: "+strDate+"[ "+ strDescription + " ]\n";
- }//for
- //mTvFeedback.setText(s);
- mTvFeedback.setText(strProcessed);
- }//onPostExecute
- }//LoadAndDisplayAsynchronously
- TextView mTvFeedback;
- AmHttpIO mHttpIO;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- public final static String TEST_URL =
- "https://arturmarques.com/edu/pdm2/dates.TSV";
- void init(){
- mHttpIO = new AmHttpIO();
- mTvFeedback = findViewById(R.id.idTvFeedback);
- /*
- valid algorithm that Android refuses to execute
- because
- "NetworkOnMainThreadException"
- solution: execute it asynchronously
- */
- try {
- loadAndDisplay(
- TEST_URL,
- mTvFeedback
- );
- }//try
- catch(Exception e){
- Toast t = Toast.makeText(
- this,
- e.toString(),
- Toast.LENGTH_LONG
- );
- t.show();
- }//catch
- loadAndDisplay2();
- }//init
- void loadAndDisplay(
- String pUrl,
- TextView pTvWhereToDisplayTheLoadedContents
- ){
- /*
- mHttpIO.consumeHttpsUrl(pUrl);
- String strContents = mHttpIO.getLastContentRead();
- */
- String strContents = mHttpIO.consumeHttpUrl(pUrl);
- //String strContents = mHttpIO.consumeHttpsUrl(pUrl);
- pTvWhereToDisplayTheLoadedContents.setText(strContents);
- }//loadAndDisplay
- void loadAndDisplay2(){
- LoadAndDisplayAsynchronously taskToConsumeHttpsAsynchronously =
- new LoadAndDisplayAsynchronously();
- taskToConsumeHttpsAsynchronously.execute(TEST_URL);
- }//loadAndDisplay2
- }//DisplayHttpContentActivity
Advertisement
Add Comment
Please, Sign In to add comment