0

The Problem

Currently I'm replacing GSON with Resty-GWT to make my JSON-RPC calls due to various annoyances with GSON. It works exactly how I want it to but I can't figure out how to send my messages other than in a String:

String names_ = "{\"jsonrpc\":\"2.0\",\"method\":\"RegisterValues\",\"params\":[[\"FirstValue\"],\"id\":2}";

I'd like to do this in a smarter way, so I don't have to write out all these values. Most importantly though, I'd like an easy way to insert those params. Some way of just declaring these properties in my request payload with ease.

Current Method

This String is sent via this call:

testService.RegisterValues(names_, new MethodCallback<testService.Response>(){

            @Override
            public void onFailure(Method method, Throwable exception) {
                // TODO Auto-generated method stub
                Window.alert(exception.getMessage().toString());
            }

            @Override
            public void onSuccess(Method method, testService.Response response) {
                // TODO Auto-generated method stub
                Window.alert("Hello" + response.getResult().toString());


            }
        });

The testService class is found here:

import javax.ws.rs.POST;
import javax.ws.rs.Produces;

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;

public interface testService extends RestService {

@Produces("application/json")
        @POST
        public void RegisterValues(String names_, MethodCallback<Response> callback );
}

The callback is then sent to this response class, where I can deserialise the data with ease, extracting (though this isn't really important here):

public class Response {

        private int id;
        private Map<String, Map<String, Integer>> result;
        private String error;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        } 
                //...etc.
fiz
  • 906
  • 3
  • 14
  • 38

1 Answers1

1

First your json does not seems right you have an open square bracket ([) not closed ?

Then you must do exactly the same thing than with your object Response. You need to create an object representing your names_ Resty will serialize it for you.

something like

public class Params {
  String jsonrpc;
  String method;
  String[] params;
  String id;
}
Ronan Quillevere
  • 3,699
  • 1
  • 29
  • 44
  • yes, sorry I missed off the brackets (this is just copied and pasted from my Eclipse project). Is there a way just to create a Java String in the JSON format to pass it over? – fiz Apr 01 '14 at 16:12
  • I must apologise too, I'm completely new to this. – fiz Apr 01 '14 at 16:21
  • I do not understand you are using string with a json object inside right now, but you do not want to use an object ? What type of object do you want to use ? – Ronan Quillevere Apr 01 '14 at 16:23
  • That `String` I have at the top of the page, instead of writing that out, I'd like to easily (/smartly) have a way of creating it with the correct parameters inside and not have to write out that string each time. Then I'd like to place this object inside `RegisterValues` and for it still to compile with GWT. – fiz Apr 01 '14 at 16:36
  • also what do I put in `InnerParam`? – fiz Apr 01 '14 at 16:39
  • Also, if I put that class Params from testService into: `testService.I2CRegisterValues(testService.params, new MethodCallback(){....`, how exactly is it declared? Could you possible write out an example to give me a sense of how to do it? – fiz Apr 01 '14 at 17:13
  • See my edit response. Params myparams = new Params(........); Then pass myparams as the first parameter of your RegisterValues method – Ronan Quillevere Apr 01 '14 at 17:18
  • One last question, that all worked and I can get all the fields set apart from I can't work out how to create this param (this is what it's meant to look like when sending in JSON-RPC): `{"jsonrpc":"2.0","method":"someMethod","params":[[]],"id":1}` Similarly: `"params":[["FirstValue","SecondValue"]]` - clearly having this as a String[] will only give `[]` when automatically encoded – fiz Apr 01 '14 at 18:31
  • did you try String [][] ? to have a table of String tables ? – Ronan Quillevere Apr 02 '14 at 07:12
  • `44:07:50.152 [ERROR] Multi-dimensional arrays are not yet supported` – fiz Apr 02 '14 at 09:20
  • you should try to change the format of your object if you can to have an object more Json friendly (with objects and not [][]) – Ronan Quillevere Apr 02 '14 at 09:26
  • It's not my server - I'm just trying to communicate to it easier. Is there a useful library you'd recommend - or phrase I can google - whereby I can change the class into a JSON-style string - as that'd work – fiz Apr 02 '14 at 09:30
  • Ahh use a Map = new HashMap, then fill it with null – fiz Apr 02 '14 at 10:00