简体   繁体   English

Android模拟器到实际设备的问题

[英]Android emulator to real device issue

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 我有这段代码是从网站获取文本,并将Android程序上的标签更改为缓冲阅读器的最后一行

EDIT - I have updated the code thanks to the kind advice of Mohsen Afshin 编辑-由于Mohsen Afshin的善意建议,我已经更新了代码

button.setOnClickListener(new View.OnClickListener() { 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. 但是,当我将其放到Android设备上时,它崩溃了。 I have changed the Android Manifest to allow internet connections - 我已更改了Android清单以允许互联网连接-

<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 - 的logcat如下-

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.918:W / dalvikvm(9047):threadid = 11:线程以未捕获的异常退出(group = 0x411432a0)06-25 16:18:15.923:E / AndroidRuntime(9047):致命异常:线程-599 06-25 16:18:15.923:E / AndroidRuntime(9047):android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。 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.setTex 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):at 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(查看.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.setTex t(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) t(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):位于android.widget.TextView.setText(TextView.java:3565)06-25 16:18:15.923:E / AndroidRuntime(9047):位于gaz.helloworld.MainActivity $ 2 $ 1.run(MainActivity.java :59)06-25 16:18:15.923:E / AndroidRuntime(9047):位于java.lang.Thread.run(Thread.java:856)

Can anyone more knowledgeable help me please? 有知识的人可以帮助我吗?

You might be getting a NetworkOnMainThreadException . 您可能会收到NetworkOnMainThreadException

Sometimes there are network problems on main UI(Activity) 有时主UI上存在网络问题(活动)

You should use a separate thread, refer to this post 您应该使用单独的线程, 请参阅这篇文章

As stated by @NinadChilap, it will generate a NetworkOnMainThreadException . 如@NinadChilap所述,它将生成NetworkOnMainThreadException

So this will work if your URL is valid: 因此,如果您的URL有效,这将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM