4

我正在开发一个 iPad 项目,这个项目需要与 json-rpc webservices 对话。网络服务基于 Drupal,带有模块:cck 和 views

1)我需要将一个json对象推送到webservice 2)我需要来自webservice的回调数据

我已经为 iPad 项目实现了 SBJSON api 和https://github.com/samuraisam/DeferredKit/ api。

SBJSON api 工作正常,我理解这个 Samuriaisam DeferedKit 对我来说是新的

我的问题是如何从这个 json-rpc 网络服务中获取数据,有人有一些示例代码吗?或者在某些地方我可以找到 Objective C - json-rpc webservice 文档。

亲切的问候,

巴特舍恩

- - - - -更新 - - - -

我现在使用这段代码:

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

这将导致来自服务器的此消息:

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

- - - - -/更新 - - - -

怎么了?有人有这方面的经验吗?

亲切的问候,

巴特舍恩

4

2 回答 2

1

读取 JSON 文件获取该数据。

NSDictionary *dictionary = [jsonString JSONValue]; 您将获得键值对。将该数据存储在您各自的变量中。

于 2010-12-20T10:13:36.073 回答
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];

    }

只是不要使用 json-rpc - 保持简单,json 是普通的 json 方法;)

于 2011-01-05T08:32:54.810 回答