我是Android开发的初学者,希望有人能帮我一点忙。
我想连接到本地服务器(IP)。我在GitHub中找到了一段代码,据推测它可以实现这种连接。但问题是,这是一个java.class而不是在我的MainActivity中。因此,当我现在在模拟器中运行我的应用程序时,什么也没有发生。如何从MainActivity内部运行Java.class
这是源代码:https://github.com/calimero-project/introduction/tree/master/src/main/java
类:
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);
}
}
}MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}发布于 2021-05-24 01:07:51
尝尝这个
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方法来建立从该类到对象的连接。
无论哪种方式,你都能做到。
您可以了解更多关于Android Development、Activity和Android Activity lifecycle的基础知识,以便更好地理解它们。
https://stackoverflow.com/questions/67660387
复制相似问题