Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class CloudHttp

  1. Object
  2. CloudHttp
Helpers shared by the REST clients: JSON bodies and the response handlers. UTF-8 conversion is com.codename1.util.StringUtil, URL encoding com.codename1.io.Util#encodeUriComponent(String) and JSON member reads com.codename1.mapping.JsonValues.

Nested types

interface CloudHttp.ReauthenticatorWhat #retryOn401(CloudHttpTransport.Handler, Reauthenticator) does when a request is refused with 401: refresh the credentials, then send the same request again without another retry.

Methods

public static byte[] toJson(Object value)Serializes a map or list to JSON bytes.
public static Map<String, Object> parseJson(byte[] body)Parses a JSON response.
public static List<Map<String, Object>> parseJsonArray(byte[] body)Parses a JSON array response into a list of objects.
public static boolean failed(AsyncResource<?> out, int status, byte[] body)Completes out with an error when status is not a 2xx code.
public static CloudHttpTransport.Handler jsonHandler(AsyncResource<Map<String, Object>> out)A handler that completes out with the parsed JSON object of a 2xx response.
public static CloudHttpTransport.Handler bytesHandler(AsyncResource<byte[]> out)A handler that completes out with the body of a 2xx response.
public static CloudHttpTransport.Handler doneHandler(AsyncResource<Boolean> out)A handler that completes out with true on a 2xx response.
public static CloudHttpTransport.Handler rowsHandler(AsyncResource<List<Map<String, Object>>> out)A handler that completes out with the rows of a 2xx JSON array response.
public static CloudHttpTransport.Handler retryOn401(CloudHttpTransport.Handler handler, CloudHttp.Reauthenticator auth)Wraps a handler so that a 401 response refreshes the credentials and resends the request once.

Inherited methods

Method details

toJson

public static byte[] toJson(Object value)
Serializes a map or list to JSON bytes.

Parameters

value Object
a Map or List

Returns

the UTF-8 JSON

parseJson

public static Map<String, Object> parseJson(byte[] body)
Parses a JSON response. A top level array is returned under the key root.

Parameters

body byte[]
the response body

Returns

the parsed map, empty when the body is empty or invalid

parseJsonArray

public static List<Map<String, Object>> parseJsonArray(byte[] body)
Parses a JSON array response into a list of objects.

Parameters

body byte[]
the response body

Returns

the rows, empty when the body holds no objects

failed

public static boolean failed(AsyncResource<?> out, int status, byte[] body)
Completes out with an error when status is not a 2xx code.

Parameters

out AsyncResource<?>
the result to fail
status int
the HTTP status
body byte[]
the response body

Returns

true when the status was an error and out was failed

jsonHandler

public static CloudHttpTransport.Handler jsonHandler(AsyncResource<Map<String, Object>> out)
A handler that completes out with the parsed JSON object of a 2xx response.

Parameters

out AsyncResource<Map<String, Object>>
the result

Returns

the handler

bytesHandler

public static CloudHttpTransport.Handler bytesHandler(AsyncResource<byte[]> out)
A handler that completes out with the body of a 2xx response.

Parameters

out AsyncResource<byte[]>
the result

Returns

the handler

doneHandler

public static CloudHttpTransport.Handler doneHandler(AsyncResource<Boolean> out)
A handler that completes out with true on a 2xx response.

Parameters

out AsyncResource<Boolean>
the result

Returns

the handler

rowsHandler

public static CloudHttpTransport.Handler rowsHandler(AsyncResource<List<Map<String, Object>>> out)
A handler that completes out with the rows of a 2xx JSON array response.

Parameters

out AsyncResource<List<Map<String, Object>>>
the result

Returns

the handler

retryOn401

public static CloudHttpTransport.Handler retryOn401(CloudHttpTransport.Handler handler, CloudHttp.Reauthenticator auth)
Wraps a handler so that a 401 response refreshes the credentials and resends the request once. Any other status, and a 401 whose refresh fails, reaches handler unchanged.

Parameters

handler CloudHttpTransport.Handler
the handler of the original request
auth CloudHttp.Reauthenticator
refreshes the credentials and resends

Returns

the wrapping handler