This topic is already covered in my post for consuming .NET web services in iOS but some questions were raised about passing arguments to the JSON-RPC methods that require them. In this short post I’ll just explain how to do just that.
Assume you have a few RPC methods that require some arguments like this.
[Jayrock.JsonRpc.JsonRpcMethod("HelloWorld")] public string HelloWorld() { return "Hello World!"; } [Jayrock.JsonRpc.JsonRpcMethod("Echo")] [Jayrock.JsonRpc.JsonRpcHelp("Simple echo method, takes string input and returns it")] public string Echo(string input) { return input; } [Jayrock.JsonRpc.JsonRpcMethod("EchoArray")] [Jayrock.JsonRpc.JsonRpcHelp("Returns input arguments as an array of strings")] public List EchoArray(string str1, string str2, string str3, string str4) { List lst = new List(); lst.Add(str1); lst.Add(str2); lst.Add(str3); lst.Add(str4); return lst; } |
Now, those “Echo” methods need some arguments passed to them and in this case I worked with strings but remember those args can be any primitive or complex but serializable types. So now that we have a working web service, we can query it from iOS. I’ll refer to my old example project which can be found here. The way it’s coded right now is to call the HelloWorld method without any arguments like this.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"HelloWorld" andParams:[NSArray array] withID:@"1"]; |
We need to notice that we passed an empty NSArray for params. This is because our HelloWorld doesn’t need any arguments. Easily enough, if you want to call the Echo method with a single param you’ll just add a string to an array and pass that array as execMethod:andParams:withID 2nd argument.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"Echo" andParams:[NSArray arrayWithObject:@"Echo Me!"] withID:@"2"]; |
You can pass as many arguments as your method requires just stack ‘em up in an NSArray and make sure the argument order complies with the argument order of the web method.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"EchoArray" andParams:[NSArray arrayWithObjects:@"1st arg", @"second arg", @"3rd arg", @"4th arg string", nil] withID:@"3"]; |
Hope this helps!
Hello Ivan, I am very glad to find your website. It helps me a lot for my iphone app develop.
For this topic, I wonder to know what should be next step for the Json result. When Ireturn an object in C# webservice, I got the object type in iOS. could you tell me how the handle the data return from the webservice which is an real object , not a string.
Thanks
Ling
Ling, what type of object do you get from JSON-RPC? if it’s JSON (and it should be) then all you need to do is to deserialize it. Once you do, you probably end up with native NSDictionary and you can do what you need with it.
Hi there…is there anyway to use this code to call a wcf webservice with 3 parameters (id, firstday, lastday) wich the url is the http://host/servicestatistics/Statistics.svc ?
sorry my bad english but i’m a non-native.
Probably not this way unless your WCF service is modified to support RPC like described here.
Thanks for this article it has really helped me out! I notice that all the json is happening in the view. For my project I would rather put that communication down in a class that the view uses. What I am having trouble with is creating a custom class that makes the JSONRPCService will use as its delegate. Any advice?
My advice is to stay away from JSON-RPC unless you really don’t have to use it
Not sure what’s your project architecture but easily you can create a wrapper for JSONRPCService and hook its delegate to your view controllers.
All the json responses are returned via the dataLoaded method. When a client has multiple types of responses to process, what is the preferred way for handling those?
All RPC methods have this ID thingy which should be used to identify your requests. I’m sure you can use that ID to know which response originates from which request.