androidweb-servicesandroid-webservice

how can i Fix this CalledFromWrongThreadException?


so i am trying to learn the android web services and when i run my code i get this Exception can you :

FATAL EXCEPTION: Thread-4
                                                                                      Process: com.example.abdallahmurad.webservices_test, PID: 7328
                                                                                      android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                                          at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6855)
                                                                                          at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1040)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:884)
                                                                                          at android.view.View.requestLayout(View.java:19654)
                                                                                          at android.widget.TextView.checkForRelayout(TextView.java:7363)
                                                                                          at android.widget.TextView.setText(TextView.java:4475)
                                                                                          at android.widget.TextView.setText(TextView.java:4332)
                                                                                          at android.widget.TextView.setText(TextView.java:4307)
                                                                                          at com.example.abdallahmurad.webservices_test.MainActivity$1$1.run(MainActivity.java:52)
                                                                                          at java.lang.Thread.run(Thread.java:761)

And here the MainActivity Code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ButterKnife.bind(this);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        URL dataPath = new URL("http://abdallahmurad.hostkda.com/myfirst_echo_test.php");
                        HttpURLConnection myFirstConnection = (HttpURLConnection) dataPath.openConnection();
                        InputStreamReader inputStreamReader = new InputStreamReader(myFirstConnection.getInputStream());
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String data = bufferedReader.readLine();
                        textView.setText(data);
                        System.out.println(data);
                        Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    });
}

the myfirst_echo_test.php is a simple php file with one echo""

my question is what is this exception and how can i fix it ?


Solution

  • textView.setText(data); causes this problem because you are trying to set the text of UI element in a non ui thread. You should change the code and do it in ui thread by calling RunOnUiThread:

      runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(data);
                             Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
                        }
                    });
    

    This will allways happen if you try to manipulate any UI element (not only a textView) inside a non ui thread (like your Runnable). As an alternative to your Runnable you can also try AsyncTask to do your actions and manipulate the UI element in onPostExecute().

     new AsyncTask<Void,Void,Void>(){
    
                 String data="";
                        @Override
                        protected Void doInBackground(Void... params) {
    
                            URL dataPath = new URL("http://abdallahmurad.hostkda.com/myfirst_echo_test.php");
                        HttpURLConnection myFirstConnection = (HttpURLConnection) dataPath.openConnection();
                        InputStreamReader inputStreamReader = new InputStreamReader(myFirstConnection.getInputStream());
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        data = bufferedReader.readLine();
    
                            return null;
                        }
    
                        @Override
                        protected void onPreExecute() {
                            super.onPreExecute();
                        }
    
                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);
                              textView.setText(data);
                              Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
                        }
                    }.execute();
    

    keep in mind that also the toast has to be called in OnPostExecute.