سؤال

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

هل كانت مفيدة؟

المحلول 2

   -(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 ;)

نصائح أخرى

Read JSon file get that data.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top