Announcement

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

    RPCRequest as JSON data

    Hi,
    I need to call a Restful web service from within smartgwt which expects/returns JSON.
    So I need for example to send this request:

    Request:
    Code:
    {"id":"ID","method":"authenticate","params":{"user":"ANDROID", "password":"PASSWORD", "client":"CLIENT"},"jsonrpc":"2.0"}
    I tried the following:

    Code:
    public void onModuleLoad() {
    
    final VLayout vlayout = new VLayout();
    IButton but = new IButton("click me");
    but.addClickHandler(new ClickHandler() {
    
    @Override
    public void onClick(ClickEvent event) {
    RPCRequest request = new RPCRequest();
    request.setActionURL("http://myWebServiceURL");
    
    request.setContentType("application/json");
    request.setUseSimpleHttp(true);
    request.setHttpMethod("POST");
    
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "ID");
    params.put("method", "authenticate");
    
    params.put("params", "\"user\":\"ANDROID\",\"password\":\"PASSWORD\", \"client\":\"CLIENT\"");
    
    params.put("jsonrpc", "2.0");
    request.setParams(params);
    
    RPCManager.sendRequest(request, new RPCCallback() {
    
    @Override
    public void execute(RPCResponse response, Object rawData,RPCRequest request) {
    
    String data = String.valueOf(rawData);
    System.out.println(data);
    }
    
    });
    
    }
    
    });
    
    vlayout.addMember(but);
    vlayout.setWidth100();
    vlayout.setHeight100();
    vlayout.draw();
    
    }
    But I get the following:
    Transport error - HTTP code: 0 for URL: http://myWebServiceURL

    In eclipse I see:

    Code:
    16:45:16.746 [ERROR] [builtinds] 16:45:16.743:XRP9:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null
    
    com.smartgwt.client.core.JsObject$SGWT_WARN: 16:45:16.743:XRP9:WARN:RPCManager:xmlHttpRequest.getAllResponseHeaders() returned null
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
        at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:72)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:296)
        at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:551)
        at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:368)
        at java.lang.Thread.run(Thread.java:745)
    and

    Code:
    16:45:16.993 [ERROR] [builtinds] 16:45:16.990:XRP9:WARN:RPCManager:Transport error - HTTP code: 0 for URL: http://myWebServiceURL - response: {operationId: &quot;custom&quot;, clientContext: undef, internalClientContext: undef, context: Obj, transactionNum: 0, httpResponseCode: 0, httpResponseText: &quot;&quot;, xmlHttpRequest: [object XMLHttpRequest], transport: &quot;xmlHttpRequest&quot;, status: -90, clientOnly: undef, httpHeaders: null, isStructured: true, callbackArgs: null, results: Obj, data: &quot;Transport error - HTTP code: 0 for URL: ...&quot;[108]}
    
    com.smartgwt.client.core.JsObject$SGWT_WARN: 16:45:16.990:XRP9:WARN:RPCManager:Transport error - HTTP code: 0 for URL: http://myWebServiceURL - response: {operationId: &quot;custom&quot;, clientContext: undef, internalClientContext: undef, context: Obj, transactionNum: 0, httpResponseCode: 0, httpResponseText: &quot;&quot;, xmlHttpRequest: [object XMLHttpRequest], transport: &quot;xmlHttpRequest&quot;, status: -90, clientOnly: undef, httpHeaders: null, isStructured: true, callbackArgs: null, results: Obj, data: &quot;Transport error - HTTP code: 0 for URL: ...&quot;[108]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:105)
        at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:72)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:296)
        at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:551)
        at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:368)
        at java.lang.Thread.run(Thread.java:745)

    Any ideas?
    I think maybe the RPCRequest is not being sent as RPC-JSON type, but how to change this?

    Using smartgwt 6.1p power
    Last edited by edulid; 20 Mar 2018, 07:50.

    #2
    HTTP code 0 is how some browsers signal that you've attempted an invalid request that never left the browser.

    In this case you've set a POST with a content type, but you haven't provided any data - you need to call setData() with the data you want to POST.

    You *did* provide params, but those would only be serialized into the request body if you had left the contentType at its default setting of "application/x-www-form-urlencoded", which is basically how an HTML form encodes a post, and is not what you want.

    Comment


      #3
      Hi Isomorphic
      thanks for pointing in the right direction. I was able to solve the issue by setting the JSON Data:
      request.setData("{...}");

      Now I need to move this functionality to the server. I see the class : com.isomorphic.rpc.RPCRequest
      but I cannot set the actionUrl or HTTPMethod. How to use this class? Am I doing something wrong?

      Comment


        #4
        or should I use standard java methods for this? HttpUrlConnection , etc ?

        Comment


          #5
          --> I solved it using standard HTTPRequest HTTPResponse objects. Thanks!

          Comment

          Working...
          X