Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class RequestBuilder
- Object
- RequestBuilder
Methods
Inherited methods
Method details
insecure
public RequestBuilder insecure(boolean insecure)Parameters
insecureboolean- true to disable ssl certificate checking
Returns
useBoolean
public RequestBuilder useBoolean(boolean useBoolean)Parameters
useBooleanboolean- true to return Boolean objects in JSON Maps
Returns
useLongs
public RequestBuilder useLongs(boolean useLongs)Parameters
useLongsboolean- true to return Long objects in JSON Maps
Returns
cacheMode
public RequestBuilder cacheMode(ConnectionRequest.CachingMode cache)com.codename1.io.ConnectionRequest#getCacheMode()Parameters
cacheConnectionRequest.CachingMode- the cache mode
Returns
postParameters
public RequestBuilder postParameters(Boolean postParameters)Parameters
postParametersBoolean- true to force post, false to use get method. Defaults to true for all methods other than GET
Returns
contentType
public RequestBuilder contentType(String s)Parameters
sString- the content type
Returns
priority
public RequestBuilder priority(byte priority)Parameters
prioritybyte- The priority.
Returns
cookiesEnabled
public RequestBuilder cookiesEnabled(boolean cookiesEnabled)Parameters
cookiesEnabledboolean- True to enable cookies. False to disable.
Returns
pathParam
public RequestBuilder pathParam(String key, String value)Parameters
keyString- the identifier key in the request.
valueString- the value to replace in the url
Returns
queryParam
public RequestBuilder queryParam(String key, String value)Parameters
keyString- param key
valueString- param value
Returns
queryParam
public RequestBuilder queryParam(String key, String[] values)Parameters
keyString- param key
valuesString[]- param values
Returns
header
public RequestBuilder header(String key, String value)Returns
body
public RequestBuilder body(String bodyContent)Parameters
bodyContentString- request body content
Returns
body
public RequestBuilder body(Data body)Parameters
bodyData- Wrapper for the request body that knows how to append to an output stream.
Returns
See also
body
public RequestBuilder body(PropertyBusinessObject body)Parameters
bodyPropertyBusinessObject- request body
Returns
onErrorCodeBytes
public RequestBuilder onErrorCodeBytes(ErrorCodeHandler<byte[]> err)Parameters
errErrorCodeHandler<byte[]>- the content of the error response
Returns
onErrorCodeJSON
public RequestBuilder onErrorCodeJSON(ErrorCodeHandler<Map> err)Parameters
errErrorCodeHandler<Map>- the content of the error response
Returns
onErrorCode
public RequestBuilder onErrorCode(ErrorCodeHandler<PropertyBusinessObject> err, Class errorClass)Parameters
errErrorCodeHandler<PropertyBusinessObject>- the content of the error response
errorClassClass- the class of the business object into which the data is parsed
Returns
followRedirects
public RequestBuilder followRedirects(boolean follow)Parameters
followboolean- false to refuse redirects
Returns
RequestBuilder instance Whether this request may follow a redirect. Requests follow them by default.
Turn it off for a request that carries CREDENTIALS. A redirect is followed with the same
headers, so a 307 hands the Authorization header – and the body – to whatever host the
response names, including an http:// one, which silently undoes a caller that was
careful to use HTTPS. A 302 or 303 is not safer, only different: it turns a POST into a
GET and the final 2xx then reports success for a write that never happened.
ConnectionRequest has always had this per request; this passes it through, which is all
that was missing.
onErrorCodeString
public RequestBuilder onErrorCodeString(ErrorCodeHandler<String> err)onError
public RequestBuilder onError(ActionListener<NetworkEvent> error)#onError(com.codename1.ui.events.ActionListener)Parameters
errorActionListener<NetworkEvent>- callback for a networking error
Returns
onError
public RequestBuilder onError(ActionListener<NetworkEvent> error, boolean replace)Parameters
errorActionListener<NetworkEvent>- callback for a networking error
replaceboolean- If true, replaces the existing errorCallback(s) with the handler provided.
Returns
timeout
public RequestBuilder timeout(int timeout)Parameters
timeoutint- request timeout in milliseconds
Returns
readTimeout
public RequestBuilder readTimeout(int timeout)ConnectionRequest#isReadTimeoutSupported()
is true on this platform.Parameters
timeoutint- The timeout.
Returns
gzip
public RequestBuilder gzip()Returns
acceptJson
public RequestBuilder acceptJson()Returns
jsonContent
public RequestBuilder jsonContent()Returns
basicAuth
public RequestBuilder basicAuth(String username, String password)Returns
bearer
public RequestBuilder bearer(String token)header("Authorization", "Bearer " + token)Parameters
tokenString- the authorization token
Returns
authorizer
public RequestBuilder authorizer(RequestAuthorizer authorizer)Gives the request an authorizer: the source of its Authorization header, renewed
and sent again once if the service answers 401. See RequestAuthorizer.
Without this call the request uses whatever
NetworkManager.setAuthorizer(String, RequestAuthorizer) registered
for its URL. A header set with header(String, String), bearer(String) or
basicAuth(String, String) wins over either. The blocking methods of this builder,
such as getAsString(), return the final answer like the asynchronous ones: a
renewal and the second attempt happen before they do.
Parameters
authorizerRequestAuthorizer- the authorizer, or
RequestAuthorizer.NONEto send this request without even the default one
Returns
fetchAsString
public ConnectionRequest fetchAsString(OnComplete<Response<String>> callback)Parameters
callbackOnComplete<Response<String>>- invoked with the result of the builder query
Returns
getAsStringAsync
public void getAsStringAsync(Callback<Response<String>> callback)#fetchAsString(com.codename1.util.OnComplete) insteadParameters
callbackCallback<Response<String>>- writes the response to this callback
getAsString
public Response<String> getAsString()Returns
fetchAsBytes
public ConnectionRequest fetchAsBytes(OnComplete<Response<byte[]>> callback)Parameters
callbackOnComplete<Response<byte[]>>- writes the response to this callback
Returns
getAsBytesAsync
public void getAsBytesAsync(Callback<Response<byte[]>> callback)#fetchAsBytes(com.codename1.util.OnComplete) insteadParameters
callbackCallback<Response<byte[]>>- writes the response to this callback
getAsBytes
public Response<byte[]> getAsBytes()Returns
fetchAsJsonMap
public ConnectionRequest fetchAsJsonMap(OnComplete<Response<Map>> callback)Parameters
callbackOnComplete<Response<Map>>- writes the response to this callback
Returns
fetchAsJsonList
public ConnectionRequest fetchAsJsonList(OnComplete<Response<List>> callback)Executes the request asynchronously when the server is expected to return
a top-level JSON array ([{...}, {...}]). Internally this funnels
through the same JSON parser as #fetchAsJsonMap(OnComplete), which
wraps top-level arrays under the synthetic key "root"; this builder
unwraps that key for you so the callback receives the array directly:
Rest.get("https://api.example.com/items")
.header("Authorization", "Bearer " + token)
.acceptJson()
.fetchAsJsonList(response -> {
List items = response.getResponseData();
renderItems(items);
});
If the server returns a JSON object instead of an array, the callback
receives an empty list. If you don’t know up-front whether the
response is an array or an object, use #fetchAsJsonMap(OnComplete)
and branch on data.get("root") instanceof List.
Parameters
callbackOnComplete<Response<List>>- writes the response (with the unwrapped list) to this callback. Always invoked on the EDT.
Returns
fetchAsMapped
public <T> ConnectionRequest fetchAsMapped(Class<T> type, OnComplete<Response<T>> callback)Executes the request asynchronously, parses the JSON response, and
hands the typed DTO to callback. Uses the build-time POJO binding
framework: type must be annotated with @Mapped (see
com.codename1.annotations.Mapped /
com.codename1.mapping.Mappers) so the build registers a typed
mapper for it.
// model
@Mapped public final class Asset {
@JsonProperty public String id;
@JsonProperty public String originalFileName;
}
// call site
Rest.get(url + "/assets/" + id)
.header("Authorization", "Bearer " + token)
.acceptJson()
.fetchAsMapped(Asset.class, response -> {
Asset a = response.getResponseData(); // already typed -- no Map casts
render(a);
});
Compared to #fetchAsJsonMap(OnComplete): no (Map) cast, no
m.get("id") boilerplate, no key-typo surprises at runtime. The
per-class mapper is generated by the Maven plugin’s
process-annotations mojo from the @Mapped annotation and lives
in <basePackage>.generated.<Class>Mapper.
If the type has no registered mapper at runtime, the listener
completes with null data and a non-200 response code is not
synthesised – inspect response.getResponseCode() to differentiate
“server returned an error” from “no mapper registered”.
Parameters
typeClass<T>- the
@Mappedclass to deserialise into callbackOnComplete<Response<T>>- invoked on the EDT with the typed result
Returns
fetchAsMappedList
public <T> ConnectionRequest fetchAsMappedList(Class<T> type, OnComplete<Response<List<T>>> callback)List-typed variant of #fetchAsMapped(Class, OnComplete). Use when
the server returns a top-level JSON array of DTOs:
Rest.get(url + "/albums")
.header("Authorization", "Bearer " + token)
.acceptJson()
.fetchAsMappedList(Album.class, response -> {
List<Album> albums = response.getResponseData();
renderAlbums(albums);
});
Internally goes through the same {"root": [...]} envelope as
#fetchAsJsonList(OnComplete), then maps each element through the
registered mapper for type.
Parameters
typeClass<T>- the per-element
@Mappedclass callbackOnComplete<Response<List<T>>>- invoked on the EDT with
List<T>data
Returns
fetchAsProperties
public ConnectionRequest fetchAsProperties(OnComplete<Response<PropertyBusinessObject>> callback, Class type)Parameters
callbackOnComplete<Response<PropertyBusinessObject>>- writes the response to this callback
typeClass- the class of the business object returned
Returns
getAsJsonMap
public ConnectionRequest getAsJsonMap(SuccessCallback<Response<Map>> callback)#fetchAsJsonMap(com.codename1.util.OnComplete) insteadParameters
callbackSuccessCallback<Response<Map>>- writes the response to this callback
Returns
getAsJsonMap
public ConnectionRequest getAsJsonMap(SuccessCallback<Response<Map>> callback, FailureCallback<? extends Object> onError)#fetchAsJsonMap(com.codename1.util.OnComplete) insteadParameters
callbackSuccessCallback<Response<Map>>- writes the response to this callback
onErrorFailureCallback<? extends Object>- the error callback
Returns
getAsJsonMapAsync
public void getAsJsonMapAsync(Callback<Response<Map>> callback)#fetchAsJsonMap(com.codename1.util.OnComplete) insteadParameters
callbackCallback<Response<Map>>- writes the response to this callback
getAsJsonMap
public Response<Map> getAsJsonMap()Returns
getAsProperties
public Response<PropertyBusinessObject> getAsProperties(Class type)Parameters
typeClass- the type of the business object to create
Returns
fetchAsPropertyList
public ConnectionRequest fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type, String root)Parameters
callbackOnComplete<Response<List<PropertyBusinessObject>>>- writes the response to this callback
typeClass- the class of the business object returned
rootString- the root element’s key of the structured content
Returns
fetchAsPropertyList
public ConnectionRequest fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type)Parameters
callbackOnComplete<Response<List<PropertyBusinessObject>>>- writes the response to this callback
typeClass- the class of the business object returned
Returns
getAsPropertyList
public Response<List<PropertyBusinessObject>> getAsPropertyList(Class type, String root)Parameters
typeClass- the type of the business object to create
rootString- the root element’s key of the structured content
Returns
getAsPropertyList
public Response<List<PropertyBusinessObject>> getAsPropertyList(Class type)Parameters
typeClass- the type of the business object to create
Returns
getRequestUrl
public String getRequestUrl()