4

I'am working on a iPad project and this project needs to talk to a json-rpc webservices. The webservices is based on Drupal with module : cck and views

1)I need to push a json object into the webservice 2)I need the callback data from the webservice

I already have implemented the SBJSON api and the https://github.com/samuraisam/DeferredKit/ api to the iPad project.

The SBJSON api works fine and I understand this one The Samuriaisam DefferedKit is new for me

My question is how to get data out of this json-rpc webservice, has someone some sample code? Or some places where I can find Objective C - json-rpc webservice documentation.

Kind Regards,

Bart Schoon

---------Update--------

I use this code now:

NSString *jsonString = @"{\"method\":\"views.get\",\"params\":{\"view_name\":\"client_list\",\"sessid\":\"xxxxxx\"},\"id\":1}";
    NSString *requestString = [NSString stringWithFormat:@"%@",jsonString,nil];

    NSLog(@"input: %@",jsonString);

    NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://subdomain.domain.com/services/json-rpc"]];

    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
    [request setHTTPMethod: @"POST"];
    [request setValue:@"Content-type: application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:requestData];

    //Data returned by WebService
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
    NSLog(@"output: %@",returnString);

This will result in this message from the server:

{"error":{"name":"JSONRPCError","code":-32600,"message":"The received JSON not a valid JSON-RPC Request"},"version":"1.1"}

---------/Update--------

What is wrong? Has someone experience with this?

Kind Regards,

Bart Schoon

Bart Schoon
  • 299
  • 5
  • 11

2 Answers2

1

Read JSon file get that data.

NSDictionary *dictionary = [jsonString JSONValue]; You will get key & value pair. Store that data in your respective variable.

Jyotsna Kadam
  • 213
  • 5
  • 12
  • Thank you for your Post, Not exactly what I am looking for, I posted some new code above. Have you got some recommendations? Kind Regards, Bart Schoon – Bart Schoon Dec 21 '10 at 09:39
  • Hi Bart, Have you checked JSon file which you are receiving whether it's valid or not. Otherwise you can try with other JSon file for temporary purpose. Still if you will get same error then have look at this link: http://groups.google.com/group/json-rpc/web/json-rpc-1-2-proposal – Jyotsna Kadam Dec 21 '10 at 10:39
  • Okay it works. I have made the choice to use the normal json method in Drupal instead of the rpc version. So now i post some POST variables to the server and receive a json string Back. Thx for helping me! – Bart Schoon Dec 24 '10 at 14:18
0
   -(IBAction)testCall{
     NSString *requestString = [NSString stringWithFormat:@"method=views.get&view_name=client_list",nil];
     NSLog(requestString);


     NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];


     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://.xxxxxxxxx.nl/services/json"]];

     NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
     [request setHTTPMethod: @"POST"];
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
     [request setHTTPBody: requestData];

     //Data returned by WebService
     NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

     NSLog(returnString);
     NSDictionary *dict = [returnString JSONValue];

    }

Just don't use a json-rpc - keep it simple and json the normal jSon method ;)

Bart Schoon
  • 299
  • 5
  • 11