com.badlogic.gdx
Interface Net


public interface Net

Provides methods to perform networking operations, such as simple HTTP get and post requests, and TCP server/client socket communication.

To perform an HTTP request create a Net.HttpRequest with the HTTP method (see Net.HttpMethods for common methods) and invoke sendHttpRequest(HttpRequest, HttpResponseListener) with it and a Net.HttpResponseListener. After the HTTP request was processed, the Net.HttpResponseListener is called with a Net.HttpResponse with the HTTP response values and an status code to determine if the request was successful or not.

To create a TCP client socket to communicate with a remote TCP server, invoke the newClientSocket(Protocol, String, int, SocketHints) method. The returned Socket offers an InputStream and OutputStream to communicate with the end point.

To create a TCP server socket that waits for incoming connections, invoke the newServerSocket(Protocol, int, ServerSocketHints) method. The returned ServerSocket offers an ServerSocket.accept(SocketHints options) method that waits for an incoming connection.

Author:
mzechner, noblemaster, arielsan

Nested Class Summary
static interface Net.HttpMethods
          Provides common HTTP methods to use when creating a Net.HttpRequest.
static class Net.HttpRequest
          Contains getters and setters for the following parameters: httpMethod: GET or POST are most common, can use HttpMethods for static references url: the url headers: a map of the headers, setter can be called multiple times timeout: time spent trying to connect before giving up content: A string containing the data to be used when processing the HTTP request. Abstracts the concept of a HTTP Request:
static interface Net.HttpResponse
          HTTP response interface with methods to get the response data as a byte[], a String or an InputStream.
static interface Net.HttpResponseListener
          Listener to be able to do custom logic once the Net.HttpResponse is ready to be processed, register it with sendHttpRequest(HttpRequest, HttpResponseListener).
static class Net.Protocol
          Protocol used by newServerSocket(Protocol, int, ServerSocketHints) and newClientSocket(Protocol, String, int, SocketHints).
 
Method Summary
 void cancelHttpRequest(Net.HttpRequest httpRequest)
           
 Socket newClientSocket(Net.Protocol protocol, String host, int port, SocketHints hints)
          Creates a new TCP client socket that connects to the given host and port.
 ServerSocket newServerSocket(Net.Protocol protocol, int port, ServerSocketHints hints)
          Creates a new server socket on the given port, using the given Net.Protocol, waiting for incoming connections.
 void openURI(String URI)
          Launches the default browser to display a URI.
 void sendHttpRequest(Net.HttpRequest httpRequest, Net.HttpResponseListener httpResponseListener)
          Process the specified Net.HttpRequest and reports the Net.HttpResponse to the specified Net.HttpResponseListener .
 

Method Detail

sendHttpRequest

void sendHttpRequest(Net.HttpRequest httpRequest,
                     Net.HttpResponseListener httpResponseListener)
Process the specified Net.HttpRequest and reports the Net.HttpResponse to the specified Net.HttpResponseListener .

Parameters:
httpRequest - The Net.HttpRequest to be performed.
httpResponseListener - The Net.HttpResponseListener to call once the HTTP response is ready to be processed. Could be null, in that case no listener is called.

cancelHttpRequest

void cancelHttpRequest(Net.HttpRequest httpRequest)

newServerSocket

ServerSocket newServerSocket(Net.Protocol protocol,
                             int port,
                             ServerSocketHints hints)
Creates a new server socket on the given port, using the given Net.Protocol, waiting for incoming connections.

Parameters:
port - the port to listen on
hints - additional ServerSocketHints used to create the socket. Input null to use the default setting provided by the system.
Returns:
the ServerSocket
Throws:
GdxRuntimeException - in case the socket couldn't be opened

newClientSocket

Socket newClientSocket(Net.Protocol protocol,
                       String host,
                       int port,
                       SocketHints hints)
Creates a new TCP client socket that connects to the given host and port.

Parameters:
host - the host address
port - the port
hints - additional SocketHints used to create the socket. Input null to use the default setting provided by the system.
Returns:
GdxRuntimeException in case the socket couldn't be opened

openURI

void openURI(String URI)
Launches the default browser to display a URI. If the default browser is not able to handle the specified URI, the application registered for handling URIs of the specified type is invoked. The application is determined from the protocol and path of the URI.

Parameters:
URI - the URI to be opened.


Copyright © 2014. All Rights Reserved.