I have this code which is getting the text from a website, and changing a label on the Android program to the last line from the buffered reader
EDIT - I have updated the code thanks to the kind advice of Mohsen Afshin
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(
"http://webpage.php");
URLConnection yc = url.openConnection();
yc.setDoOutput(true);
PrintStream ps = new PrintStream(yc
.getOutputStream());
ps.print("Name=Joe");
BufferedReader in = new BufferedReader(
new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
display.setText(inputLine);
in.close();
} catch (IOException e) { // TODO Auto-generated catch
// block
e.printStackTrace();
display.setText("failed");
}
}
});
t.start();
Now this is working absolutely fine on the emulator. However when I have put it onto an Android device it is crashing. I have changed the Android Manifest to allow internet connections -
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
I am not sure where the problem is. On a strange note, it has worked once! and will not work again. The logcat is as follows -
06-25 16:18:15.918: W/dalvikvm(9047): threadid=11: thread exiting with uncaught exception (group=0x411432a0) 06-25 16:18:15.923: E/AndroidRuntime(9047): FATAL EXCEPTION: Thread-599 06-25 16:18:15.923: E/AndroidRuntime(9047): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4925) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:950) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.widget.TextView.checkForRelayout(TextView.java:6644) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.widget.TextView.setText(TextView.java:3732) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.widget.TextView.setText(TextView.java:3590) 06-25 16:18:15.923: E/AndroidRuntime(9047): at android.widget.TextView.setText(TextView.java:3565) 06-25 16:18:15.923: E/AndroidRuntime(9047): at gaz.helloworld.MainActivity$2$1.run(MainActivity.java:59) 06-25 16:18:15.923: E/AndroidRuntime(9047): at java.lang.Thread.run(Thread.java:856)
Can anyone more knowledgeable help me please?