Class CloudHttp

java.lang.Object
com.codename1.cloud.CloudHttp

public final class CloudHttp extends Object
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.
  • Method Details

    • toJson

      public static byte[] toJson(Object value)

      Serializes a map or list to JSON bytes.

      Parameters
      • value: 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: 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: 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: the result to fail

      • status: the HTTP status

      • body: 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: 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: 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: 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: the result
      Returns

      the handler

    • retryOn401

      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: the handler of the original request
      • auth: refreshes the credentials and resends
      Returns

      the wrapping handler