0

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?

Geo1986
  • 65
  • 1
  • 2
  • 8
  • 3
    Post your log cat and the stack trace so others be able to help you – Mohsen Afshin Jun 25 '13 at 13:17
  • Hi Mohsen. Really simple question but I am very new to this. How do you get the LogCat from your Android device? – Geo1986 Jun 25 '13 at 13:29
  • @Geo1986 if it's connected to your PC via usb and you are debugging on it it should show up in DDMS and in logcat in eclipse. If somehow it's not connected this way (how are you getting the app on there?) you can use CatLog https://play.google.com/store/apps/details?id=com.nolanlawson.logcat&hl=en – Ken Wolf Jun 25 '13 at 13:32
  • If you are using Eclipse, On the Debug perspective you have a "LogCat" view (or you can add this view to any other perspective...) – Muzikant Jun 25 '13 at 13:33
  • The phones plugged in and connecting as a USB, but when I go to the option to choose a Android Device or AVD Eclipse is showing no Android device. I'm installing a (huge!) software update now which will hopefully resolve – Geo1986 Jun 25 '13 at 13:44
  • thanks for advice so far, after the update i was able to get logcat working – Geo1986 Jun 25 '13 at 15:29

2 Answers2

0

You might be getting a NetworkOnMainThreadException.

Sometimes there are network problems on main UI(Activity)

You should use a separate thread, refer to this post

Community
  • 1
  • 1
Ninad Chilap
  • 41
  • 1
  • 1
  • 5
0

As stated by @NinadChilap, it will generate a NetworkOnMainThreadException.

So this will work if your URL is valid:

Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //To change body of implemented methods use File | Settings | File Templates.
                    URL url = new URL("http://weblink.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();
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90