List of usage examples for com.google.gwt.jsonp.client JsonpRequestBuilder JsonpRequestBuilder
JsonpRequestBuilder
From source file:org.oscim.web.client.SearchBox.java
License:Open Source License
public SearchBox(final Map map) { final Button searchButton = new Button("Search"); final TextBox searchField = new TextBox(); //searchField.setText("Bremen"); final PathLayer mOverlay = new PathLayer(map, 0xCC0000FF); map.layers().add(mOverlay);/*from w w w .j a va 2 s. c o m*/ // We can add style names to widgets searchButton.addStyleName("sendButton"); RootPanel.get("nameFieldContainer").add(searchField); RootPanel.get("sendButtonContainer").add(searchButton); // Focus the cursor on the name field when the app loads searchField.setFocus(true); searchField.selectAll(); // Create a cell to render each value in the list. PoiCell poiCell = new PoiCell(); // Create a CellList that uses the cell. final CellList<PoiData> cellList = new CellList<PoiData>(poiCell, PoiData.KEY_PROVIDER); final SingleSelectionModel<PoiData> selectionModel = new SingleSelectionModel<PoiData>( PoiData.KEY_PROVIDER); cellList.setSelectionModel(selectionModel); final ScrollPanel scroller = new ScrollPanel(cellList); RootPanel.get("listContainer").add(scroller); scroller.setSize("350px", "300px"); RootPanel.get("search").getElement().getStyle().setVisibility(Visibility.VISIBLE); scroller.setVisible(false); searchField.addFocusHandler(new FocusHandler() { @Override public void onFocus(FocusEvent event) { scroller.setVisible(true); } }); selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() { public void onSelectionChange(SelectionChangeEvent event) { final PoiData d = selectionModel.getSelectedObject(); mOverlay.clearPath(); //log.debug("selected " + d.getName() + " " + d.getLatitude() + " " // + d.getLongitude()); BoundingBox b = d.getBoundingBox(); if (b != null) { if (b.maxLatitudeE6 - b.minLatitudeE6 < 100 && b.maxLongitudeE6 - b.minLongitudeE6 < 100) // for small bbox use zoom=16 to get an overview map.animator().animateTo(500, b.getCenterPoint(), 1 << 16, false); else map.animator().animateTo(b); if (d instanceof NominatimData && ((NominatimData) d).getWkt() != null) { String wkt = ((NominatimData) d).getWkt(); WKTReader r = new WKTReader(); GeometryBuffer g = new GeometryBuffer(1024, 10); try { r.parse(wkt, g); } catch (Exception e) { log.debug(wkt); } //FIXME mOverlay.setGeom(g); //log.debug("add polygon " + p.length()); } else { mOverlay.addPoint(b.maxLatitudeE6, b.minLongitudeE6); mOverlay.addPoint(b.maxLatitudeE6, b.maxLongitudeE6); mOverlay.addPoint(b.minLatitudeE6, b.maxLongitudeE6); mOverlay.addPoint(b.minLatitudeE6, b.minLongitudeE6); mOverlay.addPoint(b.maxLatitudeE6, b.minLongitudeE6); } // hide overlay after 5 seconds map.postDelayed(new Runnable() { @Override public void run() { mOverlay.clearPath(); } }, 5000); } else { MapPosition pos = new MapPosition(); map.viewport().setTilt(0); map.viewport().setRotation(0); pos.setZoomLevel(13); pos.setPosition(d.getLatitude(), d.getLongitude()); map.setMapPosition(pos); } scroller.setVisible(false); } }); // Create a handler for the sendButton and nameField class MyHandler implements ClickHandler, KeyUpHandler { /** * Fired when the user clicks on the sendButton. */ public void onClick(ClickEvent event) { sendRequest(); } /** * Fired when the user types in the nameField. */ public void onKeyUp(KeyUpEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { sendRequest(); } } /** * Send the name from the nameField to the server and wait for a * response. */ private void sendRequest() { String textToServer = searchField.getText(); searchButton.setEnabled(false); String url = URL.encode(NOMINATIM_GLOBAL + textToServer); JsonpRequestBuilder builder = new JsonpRequestBuilder(); builder.setCallbackParam("json_callback"); builder.requestObject(url, new AsyncCallback<JsArray<NominatimData>>() { public void onFailure(Throwable caught) { log.debug("request failed"); searchButton.setEnabled(true); } public void onSuccess(JsArray<NominatimData> data) { List<PoiData> items = new ArrayList<PoiData>(); for (int i = 0, n = data.length(); i < n; i++) { NominatimData d = data.get(i); items.add(d); } cellList.setRowCount(items.size(), true); cellList.setRowData(0, items); scroller.setVisible(true); searchButton.setEnabled(true); } }); } } // Add a handler to send the name to the server MyHandler handler = new MyHandler(); searchButton.addClickHandler(handler); searchField.addKeyUpHandler(handler); }
From source file:org.simbit.gwt.labyrinth.provider.rest.restlet.RestletRestImpl.java
License:Apache License
public void getJsonXss(String url, final AsyncCallback<JSONObject> callback) { JsonpRequestBuilder builder = new JsonpRequestBuilder(); builder.requestObject(url, new AsyncCallback<JavaScriptObject>() { public void onSuccess(JavaScriptObject response) { callback.onSuccess(new JSONObject(response)); }// ww w .ja va 2 s . co m public void onFailure(Throwable caught) { callback.onFailure(caught); } }); }
From source file:org.thechiselgroup.biomixer.client.workbench.util.url.JsonpUrlFetchService.java
License:Apache License
/** * This is primarily used by the {@link RetryAsyncCallbackErrorHandler} that * this uses internally.// ww w . j av a 2 s . co m * * @param url * @param callback * @param previousNumberTries */ public void fetchURL(final String url, final AsyncCallback<String> callback, int previousNumberTries) { JsonpRequestBuilder jsonp = new JsonpRequestBuilder(); // Could change timeout, but probably better to change retry attempt // number...except that exacerbates server load. Maybe longer timeout is // ok. jsonp.setTimeout(jsonp.getTimeout() * 4); jsonp.requestObject(url, new ErrorHandlingAsyncCallback<JavaScriptObject>( new RetryAsyncCallbackErrorHandler(callback, url, previousNumberTries, this)) { @Override protected void runOnSuccess(JavaScriptObject result) throws Exception { // Had trouble with injection...explicitly creating // instead. ErrorCodeJSONParser errorCodeParser = new ErrorCodeJSONParser(new JsJsonParser()); JSONObject jsonObject = new JSONObject(result); // This JSONObect method changes what appear to be // array entries to be string-integer indexed property // entries. // This happens when an array is presented without // explicit numeric indices. String jsonString = jsonObject.toString(); // Need to check for understood errors in response, such // as 403 forbidden. Integer errorCode = errorCodeParser.parse(jsonString); if (null != errorCode && 500 == errorCode) { // 500 errors don't get here! Caught lower down? // We can retry this once, since I have already seen // cases of very singular failures here. boolean retryAttempted = ((RetryAsyncCallbackErrorHandler) callback).manualRetry(); if (retryAttempted) { return; } // else if (403 == errorCode) { // // This error code, forbidden, is something I // want // // to ignore at the moment. // return; // } } else { // if (null == errorCode) { callback.onSuccess(jsonString); return; } // This wasn't a success, and we got an error code // we don't understand. // Treat as an error for the callback. callback.onFailure(new Exception("Error code, status: " + errorCode + ".")); throw new Exception("Status " + errorCode); } }); }
From source file:tigase.jaxmpp.gwt.client.dns.WebDnsResolver.java
License:Open Source License
private void resolveDomain(String domain, com.google.gwt.user.client.rpc.AsyncCallback<DnsResult> callback) { String url = getResolverUrl(); // adding parameters for resolver to process url += "?version=2"; url += "&domain=" + URL.encodeQueryString(domain); JsonpRequestBuilder builder = new JsonpRequestBuilder(); builder.requestObject(url, callback); }