Page tree

This manual will give you a walk-through on how to use JSON-RPC.

Introduction

 

Marvin supports the versions 1.0 and 1.1. of the JSON Schema Service Descriptor. The available operations and parameters of the service are discovered during the run-time.

As the JSON calls are based on the HTTP protocol, the calls can be POST or GET methods. Both are supported by Marvin. Answers of  JSON RPC call must be JSON text answers, or the RPC will not work.

The response MUST be a textual representation of any finite combinations of java.lang.Boolean, java.lang.Number, java.lang.String, java.lang.Object[], java.util.Map<java.lang.String, java.lang.Object>, and null.

Results are handled similarly as described at WSDL/SOAP RPC services.

Calling JSON-RPC Service from API

 

This is an example of how to handle JSON services via the API.

	JsonServiceDescriptor descriptor = new JsonServiceDescriptor();
    descriptor.setURL("http://api.geonames.org/");
    descriptor.setMethodName("citiesJSON");
    
    descriptor.addArgument(ServiceArgument.createArgument("north", new Double(0)));
    descriptor.addArgument(ServiceArgument.createArgument("south", new Double(0)));
    descriptor.addArgument(ServiceArgument.createArgument("east", new Double(0)));
    descriptor.addArgument(ServiceArgument.createArgument("west", new Double(0)));
    descriptor.addArgument(ServiceArgument.createArgument("language", ""));
    descriptor.addArgument(ServiceArgument.createArgument("username", ""));

    Object result = null;
    try {
        result = descriptor.getServiceHandler().callService(descriptor, 44.1, -9.9, 22.4, 55.2, "en", "demo");
    } catch (ServiceException e) {
        System.err.println("Service call failed.");
    }
    StringBuilder builder = new StringBuilder();
    Object[] array = (Object[]) ((Map<String, Object>)result).get("geonames");
    for(Object obj : array) {
        builder.append("Synchronized call returned: " + ((Map<String, Object>)obj).get("name") + "\n");
    }
    System.out.println(builder.toString());

    descriptor.getServiceHandler().callService(descriptor, new AsyncCallback<Map<String, Object>>() {

        @Override
        public void onSuccess(Map<String, Object> result) {
            StringBuilder builder = new StringBuilder();
            Object[] array = (Object[]) result.get("geonames");
            for(Object obj : array) {
                builder.append("Asynchronous call returned: " + ((Map<String, Object>)obj).get("name") + "\n");
            }
            System.out.println(builder.toString());
        }

        @Override
        public void onFailure(ServiceException caught) {
            System.err.println("Asynchronous call failed.");
        }
        
    }, 44.1, -9.9, 22.4, 55.2, "en", "demo");