Gdx.net (source)

Features

  • -

    Implementation

    Class Explanation:
  • Net.java is an interface used for the cross-platform networking. This is where you can get the objects needed to communicate with the network.
  • Socket.java is an interface that provides you with the remote socket address, connection state, and a java.io.InputStream and java.io.OutputStream to work with the socket.
  • SocketHints.java is a class used to configure TCP client sockets
  • ServerSocket.java is an interface used to create TCP server sockets. It provides the standard accept() method to get a TCP client that connected.
  • ServerSocketHints.java is a class used to configure TCP server sockets.
  • HttpStatus.java is a class used to give an easy way to see what the status code returned is.
  • HttpParameterUtils.java is a class used to provide utility methods for HTTP requests.
  • HttpRequestBuilder is a class to help with creating HttpRequests. To create a TCP client socket use this little piece of code:
    1. Socket socket = Gdx.net.newClientSocket(Protocol protocol, String host, int port, SocketHints hints);
    To create a TCP server socket use this:
    1. ServerSocket server = Gdx.net.newServerSocket(Protocol protocol, int port, ServerSocketHints hints);
    To send an HTTP Request use this:
    1. HttpRequestBuilder requestBuilder = new HttpRequestBuilder();HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("http://www.google.de").build();Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);
    To send a GET HTTP Request with arguments use this:
    1. HttpRequestBuilder requestBuilder = new HttpRequestBuilder();HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("http://www.google.de").content("q=libgdx&example=example").build();Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);
    To open the system browser use this:
    1. Gdx.net.openURI(String URI)

    Receiving Response

    There are different technique to flexibly receive response back from HTTP request as shown above. Example in Kotlin as follows.
  1. HttpResponseListener HTTPResponseListener, then supply a method parameter with instance of such class.
  1. class MyReceiverOfResult : HttpResponseListener { override fun cancelled() { // do something when request gets cancelled } override fun failed(t: Throwable?) { // do something when it fails } override fun handleHttpResponse(httpResponse: Net.HttpResponse) { // do something when gets result back }}...// assume you hold instance of such class as variable namely `receiver`.// then you just pass it to `sendHttpRequest()` methodGdx.net.sendHttpRequest(req, receiver)
  1. Anonymous object
  1. Gdx.net.sendHttpRequest(req, object: Net.HttpResponseListener { override fun cancelled() { // do something when request gets cancelled } override fun failed(t: Throwable?) { // do something when it fails } override fun handleHttpResponse(httpResponse: Net.HttpResponse) { // do something when gets result back } })

Depends on your use case and which way you find it more flexible to do so.

Notes

There are various notes needed when working with networking on different platforms.

  1. not work on GWT. This is due to java.net not being supported on GWT and there is not a viable alternative at this point other than websockets.
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. cannot access the network on the main thread without disabling strict mode. This is done to prevent network operations from hanging the main thread. See here
  4. Content-Type header for POST requests. Not all backends default to the same value (due to differences in the underlying implementations). The most common value for this header is application/x-www-form-urlencoded; however, depending on the type of data you are sending, you may need a different value (such as application/xml or application/json). See Also here here