How to get data from json-rpc web service: iPad / iPhone / Objective-C

I am working on an iPad project, and this project needs to talk to json-rpc web services. Drupal based web services with module: cck and views

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

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

SBJSON api works great and I understand this Samuriaisam DefferedKit is new to me

My question is how to get data from this json-rpc webservice, does anyone have any sample code? Or in some places where I can find documentation on the Objective-C web service - json-rpc.

Yours faithfully,

Bart Shuon

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

I am using 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 cause this message to appear from the server:

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

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

What's wrong? Does anyone come across this?

Yours faithfully,

Bart Shuon

+4
source share
2 answers
  -(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 json-rpc - keep it simple and json with the regular jSon method;)

0
source

Read the JSon file to get this data.

NSDictionary * dictionary = [jsonString JSONValue]; You will get a pair of keys and values. Store this data in the appropriate variable.

+1
source

Source: https://habr.com/ru/post/1332735/


All Articles