Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Sending RPCRequest as json from java client

    Hi,

    I am sending a RPCRequest to the server as shown below. It works fine except when I add the the statement to setContentType to json as shown below. In this case the getParameter statement in the server returns null. I am trying to set up the servlet to accept json requests.

    I am using SmartGWT Pro 5.0p.

    Client side code:

    Code:
    HashMap<String, String> h = new HashMap<String, String>();
    h.put("caller", "login");
    h.put("uid", uid);
    h.put("password", password);
    
    RPCRequest req = new RPCRequest();
    req.setParams(h);
    req.setActionURL(dataUrl);
    req.setContentType("application/json");  // works fine without this line of code
    		
    RPCManager.sendRequest(req, new RPCCallback() {
    ...
    
    });
    Server code is a servlet:

    Code:
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    // getParameter returns null when contentType is set to 'json' in client request.
    // when contentType is not explicitly set (left as x-www-form-urlencoded) then getParameter call 
    // returns value as expected
         String caller = request.getParameter("caller");
    }
    In dev console, the request looks like this

    Code:
    {
        "actionURL":"http://127.0.0.1:8888/appname/sc/Login", 
        "showPrompt":false, 
        "transport":"xmlHttpRequest", 
        "promptStyle":"cursor", 
        "params":{
            "caller":"login", 
            "password":"passwd", 
            "uid":"bc", 
            "isc_tnum":0
        }, 
        "contentType":"application/json"
    }

    #2
    Please ignore this thread.

    I was able to get the server to read from the json request by (1) setting useSimpleHttp to true and (2) encoding the request using JSONEncoder on the client side. And by creating a JSONObject from the request on the server side.
    Last edited by bc; 3 Sep 2015, 05:50.

    Comment

    Working...
    X