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 ?