Android模拟器到真正的设备问题

时间:2013-06-25 13:15:37

标签: android android-emulator

我有这个代码从网站获取文本,并将Android程序上的标签更改为缓冲读取器的最后一行

编辑 - 由于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();

现在,这在模拟器上运行得非常好。然而,当我把它放到Android设备上时,它正在崩溃。我更改了Android Manifest以允许互联网连接 -

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>

我不确定问题出在哪里。奇怪的是,它曾经工作过一次!并且不会再工作了。 logcat如下 -

06-25 16:18:15.918:W / dalvikvm(9047):threadid = 11:线程退出,未捕获异常(group = 0x411432a0) 06-25 16:18:15.923:E / AndroidRuntime(9047):致命异常:Thread-599 06-25 16:18:15.923:E / AndroidRuntime(9047):android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4925) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:950) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.view.View.requestLayout(View.java:15461) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.widget.TextView.checkForRelayout(TextView.java:6644) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.widget.TextView.setText(TextView.java:3732) 06-25 16:18:15.923:E / AndroidRuntime(9047):在android.widget.TextView.setText(TextView.java:3590) 06-25 16:18:15.923:E / AndroidRuntime(9047):在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)

更有知识的人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您可能会收到 NetworkOnMainThreadException

有时主UI(活动)上存在网络问题

您应该使用单独的帖子refer to this post

答案 1 :(得分:0)

如@NinadChilap所述,它将生成NetworkOnMainThreadException

如果您的网址有效,这将有效:

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();