所以我想学习android web服务,当我运行我的 代码我得到这个例外你可以:
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)
此处为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();
}
});
}
myfirst_echo_test.php是一个简单的php文件,带有一个echo“”
我的问题是这个例外是什么,我该如何解决?
答案 0 :(得分:1)
您只能从主线程
更新uiMainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(data);
}
});
答案 1 :(得分:0)
textView.setText(data);
导致此问题,因为您尝试在非ui线程中设置UI元素的文本。您应该通过调用RunOnUiThread
来更改代码并在ui线程中执行:
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(data);
Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
如果您尝试在非ui线程(如Runnable)中操作任何UI元素(不仅仅是textView),这将会发生。作为Runnable的替代方案,您还可以尝试AsyncTask
执行操作并操纵onPostExecute()
中的UI元素。
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();
请记住,必须在OnPostExecute
中调用toast。