GWT Ext - HTML POST request to an URL

In case that we need to send a POST requet to a specific URL, within a GWT application that uses the free framework GWT-Ext, we can do it like in this sample code:

/**
* Send a POST call to a specified URL.
*
* @param baseUrl String where we want to send the POST HTML request
*
* @param urlParms list of UrlParam objects
*/
public static void post2URL(String baseUrl, List<UrlParam> urlParms) {
    ConnectionConfig conCfg = new ConnectionConfig();
    conCfg.setUrl(baseUrl);
    conCfg.setMethod(Connection.POST);

    /* URL parameters sent with every request */
    UrlParam[] arrParam;
    if (urlParms != null) {
        arrParam = urlParms.toArray(new UrlParam[0]);
    } else {
        arrParam = new UrlParam[] {};
    }

    /* create request object */
    RequestParam reqParam = new RequestParam();
    reqParam.setParams(arrParam);

    /* create connection and perform the request */
    Connection con = new Connection(conCfg);
    con.request(reqParam);
}

So, if we want to POST a request to an URL, i.e. /myApplication/Service, to add a new service to the server list of services, we can use the above defined function like in the following example:

String url = "/myApplication/Service"
List<UrlParam> urlParms = Arrays.asList (
    new UrlParam("operation", "add"),

    new UrlParam("name", "service 123")

);

post2URL (url, urlParms);

this is the same like "myApplication/Service?operation=add&name=Service+123".

More info: Developing with Ext GWT: Enterprise RIA Development

Cheers!

3 comments:

  1. How do I set up a connection timeout XMLTreeloader?
    I would like to show an error message if the server delays the response for some reason

    ReplyDelete
  2. You can use a tree loader listener to receive notification before the HTTP call, and after (check the class TreeLoaderListener).

    On "before" function you setup a timer, and if the request takes too much time then you display and error message ... this is only half of solution, because after your chosen 'timeout' period, the server may send back the response, which you have to ignore it.

    If you find a better solution, be kind and post it here :)

    Cheers!

    ReplyDelete