public class JsonpRequestBuilder
extends java.lang.Object
<callback>(<json>);where <callback> is the url parameter (see
setCallbackParam(String)
), and
<json> is the response to the request in json format.
This will result on the client to call the corresponding AsyncCallback.onSuccess(Object)
method.
If needed, errors can be handled by a separate callback:
<failureCallback>(<error>);where <error> is a string containing an error message. This will result on the client to call the corresponding
AsyncCallback.onFailure(Throwable)
method. See
setFailureCallbackParam(String)
.
Example using JSON Google Calendar GData API:
String url = "http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full" + "?alt=json-in-script"; JsonpRequestBuilder jsonp = new JsonpRequestBuilder(); jsonp.requestObject(url, new AsyncCallback<Feed>() { public void onFailure(Throwable throwable) { Log.severe("Error: " + throwable); } public void onSuccess(Feed feed) { JsArray<Entry> entries = feed.getEntries(); for (int i = 0; i < entries.length(); i++) { Entry entry = entries.get(i); Log.info(entry.getTitle() + " (" + entry.getWhere() + "): " + entry.getStartTime() + " -> " + entry.getEndTime()); } } });This example uses these overlay types:
class Entry extends JavaScriptObject { protected Entry() {} public final native String getTitle() /*-{ return this.title.$t; }-*/; public final native String getWhere() /*-{ return this.gd$where[0].valueString; }-*/; public final native String getStartTime() /*-{ return this.gd$when ? this.gd$when[0].startTime : null; }-*/; public final native String getEndTime() /*-{ return this.gd$when ? this.gd$when[0].endTime : null; }-*/; } class Feed extends JavaScriptObject { protected Feed() {} public final native JsArray<Entry> getEntries() /*-{ return this.feed.entry; }-*/; }
Constructor and Description |
---|
JsonpRequestBuilder() |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getCallbackParam()
Returns the name of the callback url parameter to send to the server.
|
java.lang.String |
getFailureCallbackParam()
Returns the name of the failure callback url parameter to send to the
server.
|
int |
getTimeout()
Returns the expected timeout (ms) for this request.
|
JsonpRequest<java.lang.Boolean> |
requestBoolean(java.lang.String url,
AsyncCallback<java.lang.Boolean> callback) |
JsonpRequest<java.lang.Double> |
requestDouble(java.lang.String url,
AsyncCallback<java.lang.Double> callback) |
JsonpRequest<java.lang.Integer> |
requestInteger(java.lang.String url,
AsyncCallback<java.lang.Integer> callback) |
<T extends JavaScriptObject> |
requestObject(java.lang.String url,
AsyncCallback<T> callback)
Sends a JSONP request and expects a JavaScript object as a result.
|
JsonpRequest<java.lang.String> |
requestString(java.lang.String url,
AsyncCallback<java.lang.String> callback) |
void |
send(java.lang.String url)
Sends a JSONP request and does not expect any results.
|
JsonpRequest<java.lang.Void> |
send(java.lang.String url,
AsyncCallback<java.lang.Void> callback)
Sends a JSONP request, does not expect any result, but still allows to be notified when the
request has been executed on the server.
|
void |
setCallbackParam(java.lang.String callbackParam) |
void |
setFailureCallbackParam(java.lang.String failureCallbackParam) |
void |
setPredeterminedId(java.lang.String id) |
void |
setTimeout(int timeout) |
public java.lang.String getCallbackParam()
public java.lang.String getFailureCallbackParam()
public int getTimeout()
public JsonpRequest<java.lang.Boolean> requestBoolean(java.lang.String url, AsyncCallback<java.lang.Boolean> callback)
public JsonpRequest<java.lang.Double> requestDouble(java.lang.String url, AsyncCallback<java.lang.Double> callback)
public JsonpRequest<java.lang.Integer> requestInteger(java.lang.String url, AsyncCallback<java.lang.Integer> callback)
public <T extends JavaScriptObject> JsonpRequest<T> requestObject(java.lang.String url, AsyncCallback<T> callback)
JSONObject
to parse it, or use a JavaScript overlay class.public JsonpRequest<java.lang.String> requestString(java.lang.String url, AsyncCallback<java.lang.String> callback)
public void send(java.lang.String url)
public JsonpRequest<java.lang.Void> send(java.lang.String url, AsyncCallback<java.lang.Void> callback)
public void setCallbackParam(java.lang.String callbackParam)
callbackParam
- The name of the callback url parameter to send to the server. The default
value is "callback".public void setFailureCallbackParam(java.lang.String failureCallbackParam)
failureCallbackParam
- The name of the failure callback url parameter to send to the
server. The default is null.public void setPredeterminedId(java.lang.String id)
public void setTimeout(int timeout)
timeout
- The expected timeout (ms) for this request. The default is 10s.