I'm looking at jsoup libraries and i've write this simple code for watching how does it works:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadThread.start();
}
Thread downloadThread = new Thread() {
@Override
public void run() {
TextView tw = (TextView) findViewById(R.text_view.tw1);
Document doc;
try {
doc = Jsoup.connect("http://www.google.it").get();
tw.setText(doc.title());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
And i've add this element to AndroidManifest.xml
<uses-permission
android:name="android.permission.INTERNET"/>
But when i launch software, i have the sequent error:
04-27 17:25:49.801: E/AndroidRuntime(26035): FATAL EXCEPTION: Thread-2004
04-27 17:25:49.801: E/AndroidRuntime(26035):
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
created a view hierarchy can touch its views.
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:854)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.view.ViewGroup.invalidateChild(ViewGroup.java:4075)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.view.View.invalidate(View.java:10386)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.view.View.invalidate(View.java:10341)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.widget.TextView.checkForRelayout(TextView.java:6439)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.widget.TextView.setText(TextView.java:3696)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.widget.TextView.setText(TextView.java:3554)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
android.widget.TextView.setText(TextView.java:3529)
04-27 17:25:49.801: E/AndroidRuntime(26035): at
com.example.webparsingtest.MainActivity$1.run(MainActivity.java:30)