(Official website: www.libgdx.cn)
LIBGDX contains a number of cross-platform network operations classes, which are in gdx.net.
features
Cross-platform HTTP requests
Multi-platform TCP c/S socket support (configurable)
TCP/C optimization
Cross-platform browser access
Description
net.java cross-platform network interface. Get the object of network interaction here.
socket.java is an interface that provides the connection status of a remote Socket address.
sockethints.java is a TCP server-side interface that is created. Provides a standard accept () method to obtain a connected TCP client.
serversockethints.java is a class that configures a TCP server socket.
httpstatus.java is a class that looks at network status.
httpparameterutils.java is a tool method that provides HTTP requests.
httprequestbuilder Create Httprequests class.
To create a TCP client, you can use the following code:
Socket socket = Gdx.net.newClientSocket (Protocol Protocol, String host, int port, sockethints hints);
To create a TCP server side, you can use the following code:
ServerSocket Server = Gdx.net.newServerSocket (Protocol Protocol, int port, serversockethints hints);
To send an HTTP request:
Httprequestbuilder Requestbuilder = new Httprequestbuilder ();
HttpRequest HttpRequest = Requestbuilder.newrequest (). Method (httpmethods.get). URL ("http://www.google.de"). Build () ;
Gdx.net.sendHttpRequest (HttpRequest, Httpresponselistener);
To open the System browser, you can use the following code:
Gdx.net.openURI (String URI)
Note
Need attention when using different platforms.
1. The TCP client and server are used differently in GWT. The reason is that java.net does not support GWT.
2. On some platforms it is not supported to open the system browser because of specific platform limitations.
3. In Android, you must declare permissions to access the network in the Androidmanifest.xml file:
<uses-permission android:name= "Android.permission.INTERNET"/>
4. In Android, you can no longer access the network in the main thread without disabling Strictmode. Prevents the main thread from blocking.
5. Pay attention to traffic in the mobile network
6. Support Network Configuration
7, the power may be exhausted during data transmission.
8, Content-type is set to post.
(www.libgdx.cn All rights reserved, if need to reprint, indicate source)
7, LIBGDX network operation