HttpRequest with the HTTP method (see HttpMethods for common methods) and
invoke sendHttpRequest(HttpRequest, HttpResponseListener) with it and a HttpResponseListener. After the HTTP
request was processed, the HttpResponseListener is called with a 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.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | Net.
Provides common HTTP methods to use when creating a |
| public static class | Net.
Contains getters and setters for the following parameters:
|
| public static interface | Net.
HTTP response interface with methods to get the response data as a byte[], a |
| public static interface | Net.
Listener to be able to do custom logic once the |
| public static enum |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public boolean | |
| public Socket | newClientSocket(Net.
the host address host, int the port port, SocketHints additional hints)SocketHints used to create the socket. Input null to use the default setting provided by the
system.Creates a new TCP client socket that connects to the given host and port. |
| public ServerSocket | Returns: theServerSocketthe hostname or ip address to bind the socket to hostname, int the port to listen on port, ServerSocketHints additional hints)ServerSocketHints used to create the socket. Input null to use the default setting provided
by the system.Creates a new server socket on the given address and port, using the given |
| public ServerSocket | Returns: theServerSocketthe port to listen on port, ServerSocketHints additional hints)ServerSocketHints used to create the socket. Input null to use the default setting provided
by the system.Creates a new server socket on the given port, using the given |
| public boolean | |
| public void | sendHttpRequest(Net.
The httpRequest,HttpRequest to be performed.The httpResponseListenerHttpResponseListener to call once the HTTP response is ready to be processed. Could
be null, in that case no listener is called.Process the specified |
| cancelHttpRequest | back to summary |
|---|---|
| public void cancelHttpRequest(Net. | |
| isHttpRequestPending | back to summary |
|---|---|
| public boolean isHttpRequestPending(Net. | |
| newClientSocket | back to summary |
|---|---|
| public Socket newClientSocket(Net. Creates a new TCP client socket that connects to the given host and port.
| |
| newServerSocket | back to summary |
|---|---|
| public ServerSocket newServerSocket(Net. Creates a new server socket on the given address and port, using the given
| |
| newServerSocket | back to summary |
|---|---|
| public ServerSocket newServerSocket(Net. Creates a new server socket on the given port, using the given
| |
| openURI | back to summary |
|---|---|
| public boolean 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. A best effort is made to open the given URI; however, since external applications are involved, no guarantee can be made as to whether the URI was actually opened. If it is known that the URI was not opened, false will be returned; otherwise, true will be returned.
| |
| sendHttpRequest | back to summary |
|---|---|
| public void sendHttpRequest(Net. Process the specified
| |
HttpRequest.
| Modifier and Type | Field and Description |
|---|---|
| public static final String | DELETE
The DELETE method deletes the specified resource. |
| public static final String | GET
The GET method requests a representation of the specified resource. |
| public static final String | HEAD
The HEAD method asks for a response identical to that of a GET request, but without the response body. |
| public static final String | PATCH
The PATCH method is used to apply partial modifications to a resource. |
| public static final String | POST
The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. |
| public static final String | PUT
The PUT method replaces all current representations of the target resource with the request payload. |
| DELETE | back to summary |
|---|---|
| public static final String DELETE The DELETE method deletes the specified resource. | |
| GET | back to summary |
|---|---|
| public static final String GET The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. | |
| HEAD | back to summary |
|---|---|
| public static final String HEAD The HEAD method asks for a response identical to that of a GET request, but without the response body. | |
| PATCH | back to summary |
|---|---|
| public static final String PATCH The PATCH method is used to apply partial modifications to a resource. | |
| POST | back to summary |
|---|---|
| public static final String POST The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. | |
| PUT | back to summary |
|---|---|
| public static final String PUT The PUT method replaces all current representations of the target resource with the request payload. | |
HttpMethods for static
referencesMapparameters = new HashMap (); parameters.put("user", "myuser"); HttpRequest httpGet = new HttpRequest(HttpMethods.Get); httpGet.setUrl("http://somewhere.net"); httpGet.setContent(HttpParametersUtils.convertHttpParameters(parameters)); ... Gdx.net.sendHttpRequest (httpGet, new HttpResponseListener() { public void handleHttpResponse(HttpResponse httpResponse) { status = httpResponse.getResultAsString(); //do stuff here based on response } public void failed(Throwable t) { status = "failed"; //do stuff here based on the failed attempt } });
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private long | |
| private InputStream | |
| private boolean | |
| private Map | |
| private String | |
| private boolean | |
| private int | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | |
| public | HttpRequest(String
This is the HTTP method for the request, see httpMethod)HttpMethodsCreates a new HTTP request with the specified HTTP method, see |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public long | |
| public InputStream | |
| public boolean | |
| public Map | |
| public boolean | |
| public String | |
| public int | |
| public String | |
| public void | |
| public void | setContent(String
A string encoded in the corresponding Content-Encoding set in the headers, with the data to send with the
HTTP request. For example, in case of HTTP GET, the content is used as the query string of the GET while on a
HTTP POST it is used to send the POST data. content)Sets the content to be used in the HTTP request. |
| public void | setContent(InputStream
The stream with the content data. contentStream, long contentLength)Sets the content as a stream to be used for a POST for example, to transmit custom data. |
| public void | setFollowRedirects(boolean
whether to follow redirects. followRedirects)Sets whether 301 and 302 redirects are followed. |
| public void | setHeader(String
the name of the header. name, String the value of the header. value)Sets a header to this HTTP request, see |
| public void | setIncludeCredentials(boolean includeCredentials)
Sets whether a cross-origin request will include credentials. |
| public void | |
| public void | setTimeOut(int
the number of milliseconds to wait before giving up, 0 or negative to block until the operation is done timeOut)Sets the time to wait for the HTTP request to be processed, use 0 block until it is done. |
| public void |
| content | back to summary |
|---|---|
| private String content | |
| contentLength | back to summary |
|---|---|
| private long contentLength | |
| contentStream | back to summary |
|---|---|
| private InputStream contentStream | |
| followRedirects | back to summary |
|---|---|
| private boolean followRedirects | |
| headers | back to summary |
|---|---|
| private Map<String, String> headers | |
| httpMethod | back to summary |
|---|---|
| private String httpMethod | |
| includeCredentials | back to summary |
|---|---|
| private boolean includeCredentials | |
| timeOut | back to summary |
|---|---|
| private int timeOut | |
| url | back to summary |
|---|---|
| private String url | |
| HttpRequest | back to summary |
|---|---|
| public HttpRequest() | |
| HttpRequest | back to summary |
|---|---|
| public HttpRequest(String httpMethod) Creates a new HTTP request with the specified HTTP method, see
| |
| getContent | back to summary |
|---|---|
| public String getContent() Returns the content string to be used for the HTTP request. | |
| getContentLength | back to summary |
|---|---|
| public long getContentLength() Returns the content length in case content is a stream. | |
| getContentStream | back to summary |
|---|---|
| public InputStream getContentStream() Returns the content stream. | |
| getFollowRedirects | back to summary |
|---|---|
| public boolean getFollowRedirects() Returns whether 301 and 302 redirects are followed. By default true. Whether to follow redirects. | |
| getHeaders | back to summary |
|---|---|
| public Map Returns a Map | |
| getIncludeCredentials | back to summary |
|---|---|
| public boolean getIncludeCredentials() Returns whether a cross-origin request will include credentials. By default false. | |
| getMethod | back to summary |
|---|---|
| public String getMethod() Returns the HTTP method of the HttpRequest. | |
| getTimeOut | back to summary |
|---|---|
| public int getTimeOut() Returns the timeOut of the HTTP request.
| |
| getUrl | back to summary |
|---|---|
| public String getUrl() Returns the URL of the HTTP request. | |
| reset | back to summary |
|---|---|
| public void reset() Implements com. Doc from com. Resets the object for reuse. Object references should be nulled and fields may be set to default values.
| |
| setContent | back to summary |
|---|---|
| public void setContent(String content) Sets the content to be used in the HTTP request.
| |
| setContent | back to summary |
|---|---|
| public void setContent(InputStream contentStream, long contentLength) Sets the content as a stream to be used for a POST for example, to transmit custom data.
| |
| setFollowRedirects | back to summary |
|---|---|
| public void setFollowRedirects(boolean followRedirects) throws IllegalArgumentException Sets whether 301 and 302 redirects are followed. By default true. Can't be changed in the GWT backend because this uses XmlHttpRequests which always redirect.
| |
| setHeader | back to summary |
|---|---|
| public void setHeader(String name, String value) Sets a header to this HTTP request, see | |
| setIncludeCredentials | back to summary |
|---|---|
| public void setIncludeCredentials(boolean includeCredentials) Sets whether a cross-origin request will include credentials. Only used on GWT backend to allow cross-origin requests to include credentials such as cookies, authorization headers, etc... | |
| setMethod | back to summary |
|---|---|
| public void setMethod(String httpMethod) Sets the HTTP method of the HttpRequest. | |
| setTimeOut | back to summary |
|---|---|
| public void setTimeOut(int timeOut) Sets the time to wait for the HTTP request to be processed, use 0 block until it is done. The timeout is used for both the timeout when establishing TCP connection, and the timeout until the first byte of data is received.
| |
| setUrl | back to summary |
|---|---|
| public void setUrl(String url) Sets the URL of the HTTP request.
| |
String or an InputStream.
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public Map | |
| public byte[] | Returns: the result as a byte[] or null in case of a timeout or if the operation was canceled/terminated abnormally. The timeout is specified when creating the HTTP request, withHttpRequest#setTimeOut(int)Returns the data of the HTTP response as a byte[]. |
| public InputStream | |
| public String | Returns: the result as a string or null in case of a timeout or if the operation was canceled/terminated abnormally. The timeout is specified when creating the HTTP request, withHttpRequest#setTimeOut(int)Returns the data of the HTTP response as a |
| public HttpStatus |
| getHeader | back to summary |
|---|---|
| public String getHeader(String name) Returns the value of the header with the given name as a | |
| getHeaders | back to summary |
|---|---|
| public Map Returns a Map of the headers. The keys are Strings that represent the header name. Each values is a List of Strings that
represent the corresponding header values. See | |
| getResult | back to summary |
|---|---|
| public byte[] getResult() Returns the data of the HTTP response as a byte[]. Note This method may only be called once per response.
| |
| getResultAsStream | back to summary |
|---|---|
| public InputStream getResultAsStream() Returns the data of the HTTP response as an Warning
Do not store a reference to this InputStream outside of
An | |
| getResultAsString | back to summary |
|---|---|
| public String getResultAsString() Returns the data of the HTTP response as a Note This method may only be called once per response.
| |
| getStatus | back to summary |
|---|---|
| public HttpStatus getStatus() Returns the | |
HttpResponse is ready to be processed, register it with
Net#sendHttpRequest(HttpRequest, HttpResponseListener).
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | failed(Throwable
If the HTTP request failed because an Exception, t encapsulates it to give more information. t)Called if the |
| public void | handleHttpResponse(Net.
The httpResponseHttpResponse with the HTTP response values.Called when the |
| cancelled | back to summary |
|---|---|
| public void cancelled() | |
| failed | back to summary |
|---|---|
| public void failed(Throwable t) Called if the
| |
| handleHttpResponse | back to summary |
|---|---|
| public void handleHttpResponse(Net. Called when the
HttpResponseListener listener = new HttpResponseListener() {
public void handleHttpResponse (HttpResponse httpResponse) {
HttpStatus status = httpResponse.getStatus();
if (status.getStatusCode() >= 200 && status.getStatusCode() < 300) {
// it was successful
} else {
// do something else
}
}
}
| |
Net#newServerSocket(Protocol, int, ServerSocketHints) and
Net#newClientSocket(Protocol, String, int, SocketHints).
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static Net. | |
| public static Net. |
| TCP | back to summary |
|---|---|
| public static final Net. | |
| Protocol | back to summary |
|---|---|
| private Protocol() | |
| valueOf | back to summary |
|---|---|
| public static Net. | |
| values | back to summary |
|---|---|
| public static Net. | |