0

Good evning! I'm trying build an app that pass the HTML code from an URL to an InputStreamReader and set it on a TextView. I tried to do it on a different Thread.

(new Thread(new Runnable() {

            @Override
            public void run() {
                TextView textView = (TextView) findViewById(R.id.text);
                textView.setText("0.0");
                InputStream inputStream;
                URL url = null;
                InputStreamReader inputStreamReader;
                char c;
                String s = "";
                try {

                    url = new URL ("https://www.blank.org/");
                    inputStream = url.openConnection().getInputStream();
                    inputStreamReader = new InputStreamReader(inputStream);
                    do{
                        c = (char) inputStreamReader.read();
                        s+= c;
                    }while (c!=-1);
                    textView.setText(":)");
                }catch (Exception E){
                    textView.setText(":(");
                    Log.i("Error", ":(");
                }

            }
        })).start();

The app is crushing even so I have the code on try & catch, and I'm getting this error:

E/Conscrypt: ------------------Untrusted chain: ----------------------
    == Chain0 == 
     Version:   3
     Serial Number:   558b384c9801a191d4edb12f6c7f5152
E/Conscrypt:  SubjectDN:   CN=www.blank.org, OU=PositiveSSL Multi-Domain, OU=Domain Control Validated
E/Conscrypt:  IssuerDN:   CN=COMODO RSA Domain Validation Secure Server CA, O=COMODO CA Limited, L=Salford, ST=Greater Manchester, C=GB
E/Conscrypt:  Get not before:   Tue Sep 04 03:00:00 GMT+03:00 2018
     Get not after:   Sun Nov 03 01:59:59 GMT+02:00 2019
     Sig ALG name:   SHA256withRSA
     Signature:   4b60d0478a19ef41d8549347efb9ccc91438ac2261b6f248ae0260b774f2516f5f92e80dfec433ff4ba635dd20a2cc82643eb8972ae95560548a02746e22b2335e76ef68b36ea4ca1de851984e5a1b8da5f716e157ef474eeb7d8a75f61a48fad9b70124eb40e92870b772cb9299e59ebf9d44e4e359407a86c6b3615c2d2925692a62ff40aa11972f94c78a796f7a7a340aed338f2a6d8476c784430419afdbedb2921efc3601085d3cd267785d8a58d3a488a576acc213a877a80f748795e5f0056e2a3dbb8b824efc5a664eb6833ab89a8037369c021178183e6b361ba9b7f1bfc4fc2c2b579f2b0bdde7f9fa72031257c368ac3106943c1e77ea4eea96b3
E/Conscrypt:  Public key:

     30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03
     82 01 0f 00 30 82 01 0a 02 82 01 01 00 d0 78 ff 2c 2c d0 51
     d4 3a da d2 b1 aa 89 89 07 01 78 ca 67 16 b8 49 a4 39 b8 ed
     eb 48 a6 0b a3 d1 da 41 a8 82 e8 c9 55 33 ae 3c 21 24 24 e4
     b0 70 ac d8 ee 10 5c 2d aa 7d 2d e2 ef 37 ec ba 16 f9 66 a0
     c8 59 f0 03 12 6b ef d9 ee 43 43 82 05 e4 79 ea 67 a1 fc ea
     ac 57 84 cd 98 16 df dd b5 2d d1 e6 5f 43 df 1d ad fc 8a 33
     d0 4a 52 f1 43 0d 84 45 bf a3 77 90 69 37 15 bb e4 81 7a 7b
     05 81 c0 f3 ab 92 fa d9 22 17 47 f0 c1 bd f1 e4 1e db d0 bb
     93 f7 10 8a e4 81 8a 3a 3a f6 89 2d 7f 31 2a ad 8a 5d 2a ca
     25 a9 76 7b 06 83 84 ae 3f 69 64 72 a1 94 4d 39 57 fa 26 fe
     07 4a 78 82 41 8c 58 5c f0 b4 05 71 e3 69 33 10 90 00 9c 7a
     37 af dd d2 a9 3f 04 fa d8 9b 4f db 0e de 59 3a c2 21 4e d1
     68 00 47 15 f9 bc 2b 52 7d 31 31 26 f3 26 b6 27 3f 83 85 6f
     45 5c 5d 21 b9 d4 dc 60 3f 02 03 01 00 01
E/AndroidRuntime: FATAL EXCEPTION: Thread-7
    Process: com.mayli.downloadinternetcontent, PID: 7203
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8525)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1427)
        at android.view.View.requestLayout(View.java:23221)
        at android.view.View.requestLayout(View.java:23221)
        at android.view.View.requestLayout(View.java:23221)
        at android.view.View.requestLayout(View.java:23221)
        at android.view.View.requestLayout(View.java:23221)
        at android.view.View.requestLayout(View.java:23221)
        at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
        at android.view.View.requestLayout(View.java:23221)
        at android.widget.TextView.checkForRelayout(TextView.java:9789)
        at android.widget.TextView.setText(TextView.java:6023)
        at android.widget.TextView.setText(TextView.java:5849)
        at android.widget.TextView.setText(TextView.java:5806)
        at com.mayli.downloadinternetcontent.MainActivity$1.run(MainActivity.java:63)
        at java.lang.Thread.run(Thread.java:764)
D/ViewRootImpl@d02c10d[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/OpenGLRenderer: eglDestroySurface = 0x7527f284c0
D/ViewRootImpl@d02c10d[MainActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
D/ViewRootImpl@d02c10d[MainActivity]: dispatchDetachedFromWindow
D/InputEventReceiver: channel '24e04ec com.mayli.downloadinternetcontent/com.mayli.downloadinternetcontent.MainActivity (client)' ~ Disposing input event receiver.
    channel '24e04ec com.mayli.downloadinternetcontent/com.mayli.downloadinternetcontent.MainActivity (client)' ~NativeInputEventReceiver.
Application terminated.

Can you please healp me solve this problem? Thanks in advance!

Mayli
  • 93
  • 9

1 Answers1

1

Because you are accessing user interface objects through a non UI Thread. Attached is a source describing your error. Android UI Threads. I do not use android specifically.

Basically you must submit UI changes to the UI API. You usually do this in most languages with a new Runnable, or task and submit it. For android I have provided references.Activity#runOnUiThread(), Update ui from another thread in android - SO.

   (new Thread(new Runnable() {

            @Override
            public void run() {

                TextView textView = (TextView) findViewById(R.id.text);

                Runnable one = new Runnable(){
                    @Override
                    public void run() {
                        textView.setText("0.0");
                    }
                };

                Activity.runOnUiThread(one);

                InputStream inputStream;
                URL url = null;
                InputStreamReader inputStreamReader;
                char c;
                String s = "";
                try {

                    url = new URL ("https://www.blank.org/");
                    inputStream = url.openConnection().getInputStream();
                    inputStreamReader = new InputStreamReader(inputStream);
                    do{
                        c = (char) inputStreamReader.read();
                        s+= c;
                    }while (c!=-1);


                    Runnable two = new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(":)");
                        }
                    };

                    Activity.runOnUiThread(two);

                }catch (Exception E){


                    Runnable three = new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(":(");
                        }
                    };

                    Activity.runOnUiThread(three);

                    Log.i("Error", ":(");
                }

            }
        })).start();
Mr00Anderson
  • 854
  • 8
  • 16
  • 1
    Added sample, so while this may need a edit or two. This should give you an idea. Any updates on the GUI objects must be through the GUI API. – Mr00Anderson Mar 25 '19 at 19:35