Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从主活动运行java.class_Java_Android_Android Studio - Fatal编程技术网

如何从主活动运行java.class

如何从主活动运行java.class,java,android,android-studio,Java,Android,Android Studio,我是Android开发的初学者,希望有人能帮我一点忙 我想连接到本地服务器(IP)。我在GitHub中找到了一个应该可以实现这种连接的代码。但问题是这是一个java.class,而不是在我的main活动中。因此,当我现在在模拟器中运行我的应用程序时,什么都没有发生。如何从main活动中运行Java.class 资料来源如下: 类别: public class CreateTunnelingLink { /** * Local endpoint, replace the IP

我是Android开发的初学者,希望有人能帮我一点忙

我想连接到本地服务器(IP)。我在GitHub中找到了一个应该可以实现这种连接的代码。但问题是这是一个
java.class
,而不是在我的
main活动中。因此,当我现在在模拟器中运行我的应用程序时,什么都没有发生。如何从
main活动中运行
Java.class

资料来源如下:

类别:

public class CreateTunnelingLink
{

    /**
     * Local endpoint, replace the IP address with your actual address. The local socket address is important for
     * multi-homed clients (several network interfaces), or if the address via InetAddress.getLocalHost is not useful.
     */
    private static final InetSocketAddress local = new InetSocketAddress("192.168.10.13", 3671);

    /**
     * Specifies the KNXnet/IP server to access the KNX network, insert your server's actual host name or IP address,
     * e.g., "192.168.1.20". The default port is where most servers listen on for new connection requests.
     */
    private static final InetSocketAddress server = new InetSocketAddress("myKnxServer.myHome",
            KNXnetIPConnection.DEFAULT_PORT);

    public static void main(final String[] args)
    {
        System.out.println("This example establishes a tunneling connection to the KNXnet/IP server " + server);

        // A KNX tunneling link supports NAT (Network Address Translation) if required.
        // We also indicate that the KNX installation uses twisted-pair (TP) medium, with TP1 being the most common.
        // KNXNetworkLink is the base interface implemented by all supported Calimero links to a KNX network.
        try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(local, server, false, TPSettings.TP1)) {
            System.out.println("Connection established to server " + knxLink.getName());
            System.out.println("Close connection again");
        }
        catch (KNXException | InterruptedException e) {
            // KNXException: all Calimero-specific checked exceptions are subtypes of KNXException

            // InterruptedException: longer tasks that might block are interruptible, e.g., connection procedures. In
            // such case, an instance of InterruptedException is thrown.
            // If a task got interrupted, Calimero will clean up its internal state and resources accordingly.
            // Any deviation of such behavior, e.g., where not feasible, is documented in the Calimero API.

            System.out.println("Error creating KNXnet/IP tunneling link: " + e);
        }
    }
}

主要活动:


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


}
试试这个

public class MainActivity extends AppCompatActivity {

    private static final InetSocketAddress local = new InetSocketAddress("192.168.10.13", 3671);
    private static final InetSocketAddress server = new InetSocketAddress("myKnxServer.myHome",
            KNXnetIPConnection.DEFAULT_PORT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // call your method to do the task
        yourMethodForMakingConnection();
    }

// for better separation you can create a method for this task
private void yourMethodForMakingConnection() {
        System.out.println("This example establishes a tunneling connection to the KNXnet/IP server " + server);

        // A KNX tunneling link supports NAT (Network Address Translation) if required.
        // We also indicate that the KNX installation uses twisted-pair (TP) medium, with TP1 being the most common.
        // KNXNetworkLink is the base interface implemented by all supported Calimero links to a KNX network.
        try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(local, server, false, TPSettings.TP1)) {
            System.out.println("Connection established to server " + knxLink.getName());
            System.out.println("Close connection again");
        }
        catch (KNXException | InterruptedException e) {
            // KNXException: all Calimero-specific checked exceptions are subtypes of KNXException

            // InterruptedException: longer tasks that might block are interruptible, e.g., connection procedures. In
            // such case, an instance of InterruptedException is thrown.
            // If a task got interrupted, Calimero will clean up its internal state and resources accordingly.
            // Any deviation of such behavior, e.g., where not feasible, is documented in the Calimero API.

            System.out.println("Error creating KNXnet/IP tunneling link: " + e);
        }
}


}
如果代码正确,这应该可以工作。将
Activities
onCreate
功能视为主要功能。对于这么小的任务,您不需要另一个类

或者,您也可以在
MainActivity
中的
onCreate
方法中创建该类的实例,然后调用
main
方法,建立该类与对象的连接

不管怎样,你都能做到


您可以了解更多关于的基础知识,以便更好地理解这一点。

您可以包含java类和主要活动的代码吗?我已经添加了代码,我在主要活动中没有做任何事情,因为我不知道应该做什么。嗨,现在我可以运行我的应用程序了,所以我认为这是可行的。但是我得到一个错误:
I/System.out:创建KNXnet/IP隧道链接时出错:tuwien.auto.calimero.KNXException:从/192.168.10.118:3671连接到/192.168.10.13:3671:套接字失败:EPERM(不允许操作)
。也许我把IP地址弄错了。我知道服务器是192.168.10.13:3671,电脑是192.168.10.118,但可能端口错误?请尝试从您的设备完全卸载应用程序,然后重新安装,确保您的清单文件中也有Internet权限,如果没有,请添加这两个:我已经尝试过,我的清单中有这两个。我知道这个错误,它和上次不一样<代码>:从/192.168.10.118:3671连接到/192.168.10.13:3671:bind失败:EADDRNOTAVAIL(无法分配请求的地址)
。我将接受您的答案,因为这是我首先要问的问题。是的,您不能在主线程上进行网络调用。您可以使用诸如Reformation之类的库来为您处理事情,但是您仍然需要学习如何利用代码,所以没有什么可以逃避的!