Example usage for com.google.gwt.http.client RequestBuilder sendRequest

List of usage examples for com.google.gwt.http.client RequestBuilder sendRequest

Introduction

In this page you can find the example usage for com.google.gwt.http.client RequestBuilder sendRequest.

Prototype

public Request sendRequest(String requestData, RequestCallback callback) throws RequestException 

Source Link

Document

Sends an HTTP request based on the current builder configuration with the specified data and callback.

Usage

From source file:com.google.gerrit.client.rpc.RestApi.java

License:Apache License

private <T extends JavaScriptObject> void sendJSON(Method method, JavaScriptObject content,
        AsyncCallback<T> cb) {/*from  w  w w . j  a  v a 2  s .  com*/
    HttpCallback<T> httpCallback = new HttpCallback<T>(cb);
    try {
        RpcStatus.INSTANCE.onRpcStart();
        String body = new JSONObject(content).toString();
        RequestBuilder req = request(method);
        req.setHeader("Content-Type", JSON_UTF8);
        req.sendRequest(body, httpCallback);
    } catch (RequestException e) {
        httpCallback.onError(null, e);
    }
}

From source file:com.google.gwt.examples.http.client.GetExample.java

public static void doGet(String url) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {//from  www. j  a  v a 2s.  co m
        Request response = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                // Code omitted for clarity
            }

            public void onResponseReceived(Request request, Response response) {
                // Code omitted for clarity
            }
        });
    } catch (RequestException e) {
        // Code omitted for clarity
    }
}

From source file:com.google.gwt.examples.http.client.PostExample.java

public static void doPost(String url, String postData) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

    try {/*from  www.j a v  a  2  s . c  o m*/
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
        Request response = builder.sendRequest(postData, new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                // code omitted for clarity
            }

            public void onResponseReceived(Request request, Response response) {
                // code omitted for clarity
            }
        });
    } catch (RequestException e) {
        Window.alert("Failed to send the request: " + e.getMessage());
    }
}

From source file:com.google.gwt.examples.http.client.TimeoutExample.java

public static void doGetWithTimeout(String url) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {/* w  ww . j  av  a  2  s . c  om*/
        /*
         * wait 2000 milliseconds for the request to complete
         */
        builder.setTimeoutMillis(2000);

        Request response = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                if (exception instanceof RequestTimeoutException) {
                    // handle a request timeout
                } else {
                    // handle other request errors
                }
            }

            public void onResponseReceived(Request request, Response response) {
                // code omitted for clarity
            }
        });
    } catch (RequestException e) {
        Window.alert("Failed to send the request: " + e.getMessage());
    }
}

From source file:com.google.gwt.sample.client.mystockwatcherEntryPoint.java

/**
 * Generate random stock prices./*from  w w w  .j  a  v a 2  s  .  c o  m*/
 */
private void refreshWatchList() {
    if (stocks.size() == 0) {
        return;
    }

    String url = JSON_URL;

    // Append watch list stock symbols to query URL.
    Iterator iter = stocks.iterator();
    while (iter.hasNext()) {
        url += iter.next();
        if (iter.hasNext()) {
            url += "+";
        }
    }

    url = URL.encode(url);

    // Send request to server and catch any errors.
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {
        Request request = builder.sendRequest(null, new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                displayError("Couldn't retrieve JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    updateTable(asArrayOfStockData(response.getText()));
                } else {
                    displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        displayError("Couldn't retrieve JSON");
    }
}

From source file:com.google.gwt.sample.healthyeatingapp.client.FacebookGraph.java

private void QueryGraph(String id, Method method, String params,
        final Callback<JSONObject, Throwable> callback) {
    final String requestData = "access_token=" + token + (params != null ? "&" + params : "");
    RequestBuilder builder;
    if (method == RequestBuilder.POST) {
        builder = new RequestBuilder(method, "https://graph.facebook.com/" + id);
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
    } else if (method == RequestBuilder.GET) {
        builder = new RequestBuilder(method, "https://graph.facebook.com/" + id + "?" + requestData);
    } else {/* w w  w  .ja v  a2s .  c o  m*/
        callback.onFailure(new IOException("doGraph only supports GET and POST requests"));
        return;
    }
    try {
        builder.sendRequest(requestData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                callback.onFailure(exception);
            }

            public void onResponseReceived(Request request, Response response) {
                if (Response.SC_OK == response.getStatusCode()) {
                    callback.onSuccess(JSONParser.parseStrict(response.getText()).isObject());
                } else if (Response.SC_BAD_REQUEST == response.getStatusCode()) {
                    callback.onFailure(new IOException("Error: " + response.getText()));
                } else {
                    callback.onFailure(
                            new IOException("Couldn't retrieve JSON (" + response.getStatusText() + ")"));
                }
            }

        });
    } catch (RequestException e) {
        callback.onFailure(e);
    }
}

From source file:com.google.gwt.sample.simplexml.client.SimpleXML.java

License:Apache License

public void onModuleLoad() {
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, "customerRecord.xml");

    try {//from  www .j ava  2 s .  c o  m
        requestBuilder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                requestFailed(exception);
            }

            public void onResponseReceived(Request request, Response response) {
                renderXML(response.getText());
            }
        });
    } catch (RequestException ex) {
        requestFailed(ex);
    }
}

From source file:com.google.gwt.sample.stockwatcher.client.PetrolTracker.java

/**
 * Add stock to FlexTable. Executed when the user clicks the addStockButton or
 * presses enter in the newSymbolTextBox.
         /*from w w  w. ja v  a  2  s.  c  o  m*/
private void addFillups() {
  final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
  newSymbolTextBox.setFocus(true);
        
  // Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
  if (!symbol.matches("^[0-9a-zA-Z\\.]{1,10}$")) {
    Window.alert("'" + symbol + "' is not a valid symbol.");
    newSymbolTextBox.selectAll();
    return;
  }
        
  newSymbolTextBox.setText("");
        
  // Don't add the stock if it's already in the table.
  if (stocks.contains(symbol))
    return;
        
  // Add the stock to the table.
  int row = fillupFlexTable.getRowCount();
  stocks.add(symbol);
  fillupFlexTable.setText(row, 0, symbol);
  fillupFlexTable.setWidget(row, 2, new Label());
  fillupFlexTable.getCellFormatter().addStyleName(row, 1, "watchListNumericColumn");
  fillupFlexTable.getCellFormatter().addStyleName(row, 2, "watchListNumericColumn");
  fillupFlexTable.getCellFormatter().addStyleName(row, 3, "watchListRemoveColumn");
        
  // Add a button to remove this stock from the table.
  Button removeStockButton = new Button("x");
  removeStockButton.addStyleDependentName("remove");
  removeStockButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
int removedIndex = stocks.indexOf(symbol);
stocks.remove(removedIndex);
fillupFlexTable.removeRow(removedIndex + 1);
    }
  });
  fillupFlexTable.setWidget(row, 3, removeStockButton);
        
  // Get the stock price.
  refreshWatchList();
        
}
*/
private void refreshFillups() {
    String url = JSON_URL;

    final String encodedUrl = URL.encode(url);
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, encodedUrl);
    try {
        @SuppressWarnings("unused")
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                displayError("Couldn't retrieve JSON on url:" + encodedUrl);
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    String jsonString = response.getText();
                    updateTable(asArrayOfFillupData(jsonString));
                } else {
                    displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        displayError("Couldn't retrieve JSON");
    }

}

From source file:com.google.gwt.sample.stockwatcher.client.ui.StockWatcherViewImpl.java

/**
 * Generate random stock prices./*  w  w  w  .ja va2  s . co m*/
 */
private void refreshWatchList() {
    if (stocks.size() == 0) {
        return;
    }
    String url = JSON_URL;

    // Append watch list stock symbols to query URL.
    Iterator iter = stocks.iterator();
    while (iter.hasNext()) {
        url += iter.next();
        if (iter.hasNext()) {
            url += "+";
        }
    }

    url = URL.encode(url);

    // Send request to server and catch any errors.
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                displayError("Couldn't retrieve JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    updateTable(asArrayOfStockData(response.getText()));
                } else {
                    displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        displayError("Couldn't retrieve JSON");
    }
}

From source file:com.google.gwt.sample.stockwatcher_json.client.StockWatcherJSON.java

private void randomizeNumber() {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, JSON_RANDOMIZE);

    try {/*from  w  w w.jav a2s. co m*/
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                displayError("Couldn't retrieve JSON : " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    StockWatcherJSON.this.randomizeLabel.setText(response.getText());
                    StockWatcherJSON.this.randomizeLabel.setVisible(true);
                } else {
                    displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                    StockWatcherJSON.this.randomizeLabel.setVisible(false);
                }
            }
        });
    } catch (RequestException e) {
        displayError("Couldn't retrieve JSON : " + e.getMessage());
        StockWatcherJSON.this.randomizeLabel.setVisible(false);
    }
}