“android.os.NetworkOnMainThreadException”尝试使用Calim

2019-07-03 15:49发布

我使用的是Android应用使用的开源Java库(Calimero)。 当我尝试通过WiFi连接到KNXnet / IP路由器我的代码引发埃罗。

问题就在这里的代码:

private static KNXNetworkLinkIP connect(InetSocketAddress isaLocalEP, InetSocketAddress isaRemoteEP)
  {
    KNXNetworkLinkIP netLinkIp = null;

    int serviceMode = KNXNetworkLinkIP.TUNNEL; // tunnel to IP router
    boolean useNAT = true; // NAT not used for PC true or false , but needed for emulator = true
    KNXMediumSettings tpSettings = new TPSettings(true); // TP1 medium

    try
    {
      // Output the local end point address

      if (m_debugOutput == true)
      {
        System.out.println("..Tunneling, NAT ignored, TP1 medium");

        // Should be the PC's VPN address

        System.out.print("..Local  EP:");
        System.out.println(isaLocalEP.getHostName() + ":" + isaLocalEP.getPort());

        System.out.print("..Remote EP:");
        System.out.println(isaRemoteEP.getHostName() + ":" + isaRemoteEP.getPort());

        System.out.print("..useNAT:");
        System.out.println(useNAT);

        System.out.println();
      }

      netLinkIp = new KNXNetworkLinkIP(serviceMode, isaLocalEP, isaRemoteEP, useNAT, tpSettings);
    }
    catch (KNXLinkClosedException e)
    {
      System.out.println("connect:KNXLinkClosedException = " + e.getMessage());
    }
    catch (KNXFormatException e)
    {
      e.printStackTrace();
      System.out.println("connect:KNXFormatException = " + e.getMessage());
    }
    catch (KNXException e)
    {
       e.printStackTrace();
      System.out.println("connect:KNXException = " + e.getMessage());
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.out.println("connect:Exception = " + e.getMessage());
    }
    return netLinkIp;

  } // connect(isaLocalEP, isaRemoteEP)

它抛出这个错误KNXException e

11-07 10:16:32.085: W/System.err(17185): android.os.NetworkOnMainThreadException
11-07 10:16:32.090: W/System.err(17185):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-07 10:16:32.090: W/System.err(17185):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175)
11-07 10:16:32.090: W/System.err(17185):    at libcore.io.IoBridge.sendto(IoBridge.java:463)
11-07 10:16:32.090: W/System.err(17185):    at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
11-07 10:16:32.090: W/System.err(17185):    at java.net.DatagramSocket.send(DatagramSocket.java:287)
11-07 10:16:32.090: W/System.err(17185):    at tuwien.auto.calimero.knxnetip.ConnectionImpl.connect(ConnectionImpl.java:360)
11-07 10:16:32.090: W/System.err(17185):    at tuwien.auto.calimero.knxnetip.KNXnetIPTunnel.<init>(KNXnetIPTunnel.java:117)
11-07 10:16:32.090: W/System.err(17185):    at tuwien.auto.calimero.link.KNXNetworkLinkIP.<init>(KNXNetworkLinkIP.java:179)
11-07 10:16:32.090: W/System.err(17185):    at com.example.connectiontest.KNXNetwork.connect(KNXNetwork.java:148)
11-07 10:16:32.090: W/System.err(17185):    at com.example.connectiontest.KNXNetwork.connect(KNXNetwork.java:121)
11-07 10:16:32.090: W/System.err(17185):    at com.example.connectiontest.KNXNetwork.tryConnecting(KNXNetwork.java:79)
11-07 10:16:32.090: W/System.err(17185):    at com.example.connectiontest.HelloKNXNetwork.connect(HelloKNXNetwork.java:67)
11-07 10:16:32.090: W/System.err(17185):    at com.example.connectiontest.HelloKNXNetwork.onClickActivity(HelloKNXNetwork.java:42)
11-07 10:16:32.090: W/System.err(17185):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 10:16:32.090: W/System.err(17185):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 10:16:32.095: W/System.err(17185):    at android.view.View$1.onClick(View.java:3111)
11-07 10:16:32.095: W/System.err(17185):    at android.view.View.performClick(View.java:3644)
11-07 10:16:32.095: W/System.err(17185):    at android.view.View$PerformClick.run(View.java:14313)
11-07 10:16:32.095: W/System.err(17185):    at android.os.Handler.handleCallback(Handler.java:605)
11-07 10:16:32.095: W/System.err(17185):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 10:16:32.095: W/System.err(17185):    at android.os.Looper.loop(Looper.java:137)
11-07 10:16:32.095: W/System.err(17185):    at android.app.ActivityThread.main(ActivityThread.java:4514)
11-07 10:16:32.095: W/System.err(17185):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 10:16:32.095: W/System.err(17185):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 10:16:32.095: W/System.err(17185):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-07 10:16:32.095: W/System.err(17185):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-07 10:16:32.095: W/System.err(17185):    at dalvik.system.NativeStart.main(Native Method)

从ConnectionImpl.java 360线socket.send(p);

从此方法:

protected void connect(InetSocketAddress localEP, InetSocketAddress serverCtrlEP,
        CRI cri, boolean useNAT) throws KNXException
    {
        ctrlEP = serverCtrlEP;
        isNatAware = useNAT;
        logger = LogManager.getManager().getLogService(getName());
        try {
            // if we allow localEP to be null, we would create an unbound socket
            if (localEP == null)
                throw new KNXIllegalArgumentException("no local endpoint specified");
            logger.info("establish link from " + localEP + " to " + ctrlEP);
            socket = new DatagramSocket(localEP);
            // HPAI throws if wildcard local address (0.0.0.0) is supplied
            final HPAI hpai =
                new HPAI(HPAI.IPV4_UDP, isNatAware ? null : (InetSocketAddress) socket
                    .getLocalSocketAddress());
            final byte[] buf = PacketHelper.toPacket(new ConnectRequest(cri, hpai, hpai));
            final DatagramPacket p =
                new DatagramPacket(buf, buf.length, ctrlEP.getAddress(), ctrlEP.getPort());
            socket.send(p);
        }
        catch (final IOException e) {
            if (socket != null)
                socket.close();
            logger.error("communication failure on connect", e);
            if (localEP.getAddress().isLoopbackAddress())
                logger.warn("try to specify the actual IP address of the local host");
            LogManager.getManager().removeLogService(getName());
            throw new KNXException(e.getMessage());
        }
        logger.info("wait for connect response from " + ctrlEP + " ...");
        startReceiver();
        final boolean changed = waitForStateChange(CLOSED, CONNECT_REQ_TIMEOUT);
        if (state == OK) {
            (heartbeat = new HeartbeatMonitor()).start();
            logger.info("link established");
            return;
        }
        // quit, cleanup and notify client
        receiver.quit();
        socket.close();
        KNXException e;
        if (!changed)
            e = new KNXTimeoutException("timeout connecting to control endpoint "
                    + ctrlEP);
        else if (state == ACK_ERROR)
            e = new KNXRemoteException(
                "acknowledge error, failed to connect to control endpoint " + ctrlEP);
        else
            e = new KNXInvalidResponseException("invalid connect response from " + ctrlEP);
        setState(CLOSED);
        logger.error("establishing connection failed", e);
        LogManager.getManager().removeLogService(getName());
        throw e;
    }

可能是什么问题呢?

Answer 1:

你得异常清楚地说,你不能有UI线程上的网络操作。 曾经的AsyncTask代替。 请参见这里 ,并得到一个例子在这里



Answer 2:

它不是无线网络连接的问题。 从Android 3.0的起网络运营不允许在UI线程(主线程)。 你必须做一个单独的线程的网络通信。
见这
即使您正在使用的Android版本低于3.0,任何网络操作持续超过5秒 (即如果UI threda没有响应超过5秒),将强制关闭应用程序。 因此,应尽量避免。



文章来源: “android.os.NetworkOnMainThreadException” when trying to connect over WiFi using Calimero Java Library