Można użyć GWT RPC w następujący sposób na kliencie:
Tworzenie usługi
private void refreshWatchList() {
final double MAX_PRICE = 100.0; // $100.00
final double MAX_PRICE_CHANGE = 0.02; // +/- 2%
StockPrice[] prices = new StockPrice[stocks.size()];
for (int i = 0; i < stocks.size(); i++) {
double price = Random.nextDouble() * MAX_PRICE;
double change = price * MAX_PRICE_CHANGE
* (Random.nextDouble() * 2.0 - 1.0);
prices[i] = new StockPrice(stocks.get(i), price, change);
}
updateTable(prices);
}
Definiowanie usługi
@RemoteServiceRelativePath("stockPrices")
public interface StockPriceService extends RemoteService {
StockPrice[] getPrices(String[] symbols);
}
Realizacja usługi
public class StockPriceServiceImpl extends RemoteServiceServlet implements StockPriceService {
public StockPrice[] getPrices(String[] symbols) {
// TODO Auto-generated method stub
return null;
}
}
powołując się na usługę z klientem i dokonywania zdalne wywołanie procedury:
private ArrayList<String> stocks = new ArrayList<String>();
private StockPriceServiceAsync stockPriceSvc = GWT.create(StockPriceService.class);
private void refreshWatchList() {
// Initialize the service proxy.
if (stockPriceSvc == null) {
stockPriceSvc = GWT.create(StockPriceService.class);
}
// Set up the callback object.
AsyncCallback<StockPrice[]> callback = new AsyncCallback<StockPrice[]>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
}
public void onSuccess(StockPrice[] result) {
updateTable(result);
}
};
// Make the call to the stock price service.
stockPriceSvc.getPrices(stocks.toArray(new String[0]), callback);
}
Jest to w zasadzie co RPC jest wykonywana.
Twoje
klucze w środkowej
jest prawdopodobnie wynikiem serializacji obiektami. Jeśli nie podoba ci się to, możesz również użyć GWT RequestBuilder
.
RequestBuilder requestBuilder = new RequestBuilder(requestMethod, url);
requestBuilder.setHeader("Content-Type", "application/json");
requestBuilder.setRequestData(bodyString);
requestBuilder.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
callback.onResponse(response.getStatusCode(), response.getText());
}
@Override
public void onError(Request request, Throwable exception) {
callback.onError(new Exception(exception));
}
});
try {
requestBuilder.send();
}
catch(RequestException ex) {
callback.onError(ex);
}
Korzystając z modułu RequestBuilder, masz większą kontrolę nad formatem i transportem.
Proszę dodać sygnaturę metody zdalnej procedury, do której dzwonisz. – Baz
Czy * ty * wykonałeś jakieś badania przed zadaniem tego pytania? GWT jest open source i powinno być dość łatwe do wyśledzenia "winowajcy" - sprawdź klasy wygenerowane z twoich interfejsów '* Async' przez kompilator GWT, zobacz jakie klasy są tam używane i podążaj szlakiem dopóki nie znajdziesz schematu serializacji . Lub w Google ... –