android线程访问网络报错
PHP中文网
PHP中文网 2017-04-17 13:31:42
0
3
606

我在写一个欢迎界面的时候,报了下面的错误,感觉很奇怪。明明是在线程中访问了网络,怎么还会报这个错误

05-22 21:42:32.855 15254-15254/com.zhu.teacherhelper E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.zhu.teacherhelper, PID: 15254
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:276)
at libcore.io.IoBridge.sendto(IoBridge.java:513)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
at java.net.DatagramSocket.send(DatagramSocket.java:305)
at com.zhu.teacherhelper.utils.ServerAddressUtils.AskServerAddress(ServerAddressUtils.java:39)
at com.zhu.teacherhelper.ui.WelcomeActivity$AskServerIP.run(WelcomeActivity.java:31)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5233)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    Handler handler = new Handler();
    //延迟一秒后进行
    handler.postDelayed(new AskServerIP(), 1000);

}

class AskServerIP implements Runnable{

    @Override
    public void run() {
        //获取服务器ip地址
        ServerAddressUtils.AskServerAddress(getApplicationContext());

        Intent intent = new Intent(WelcomeActivity.this,MouseActivity.class);
        startActivity(intent);
        WelcomeActivity.this.finish();
    }
}
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
洪涛

After 4.0, network operations cannot be placed on the main thread and must be operated asynchronously to avoid causing interface blocking.
Since you are using Handler to perform network operations, the following solution is given:

javaHandlerThread thread = new HandlerThread("NetWork");
thread.start();
Handler handler = new Handler(thread.getLooper());
//延迟一秒后进行
handler.postDelayed(new AskServerIP(), 1000);

Just change it to this.

One thing to note: it is either a Handler or an asynchronous thread. The most important thing about a Handler is its looper. If the looper is not passed in during construction, the looper of the thread where the Handler is built will be used by default.
The place where you build the looper is in the onCreate method, which is the main thread, so an error is reported.

For knowledge in this area, you can search below.
There are related tutorials on the MOOC website, you can see the following: http://www.imooc.com/learn/267

伊谢尔伦

This problem began to appear in Android 4.0. The purpose is to remove time-consuming network operations from the main thread, increase the timely response of the application, and not block user interaction. The solution is to put the network request into an asynchronous thread for processing. For details, you can refer to this article to talk about NetworkOnMainThreadException in android

Peter_Zhu

Your code does not open any new threads

  Handler handler = new Handler();
    //延迟一秒后进行
    handler.postDelayed(new AskServerIP(), 1000);

Your Handler is created in the main thread. When using the no-argument constructor, the Handler will be bound to the thread that created the Handler.

Then you post a Runnable to be executed on the thread bound to the Handler.

So actually AskServerIP is ultimately executed on the main thread.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template