如何从 json-rpc Web 服务获取数据:iPad /iPhone / Objective C

发布于 2024-10-08 18:17:58 字数 1947 浏览 6 评论 0原文

我正在开发一个 iPad 项目,该项目需要与 json-rpc Web 服务通信。 Web 服务基于 Drupal,带有模块:cck 和视图

1)我需要将 json 对象推送到 Web 服务中 2)我需要来自网络服务的回调数据

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

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

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

亲切的问候,

Bart Schoon

---------更新--------

我现在使用此代码:

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"}

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

出了什么问题?有人有这方面的经验吗?

亲切的问候,

Bart Schoon

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-10-15 18:17:58

读取 JSON 文件获取该数据。

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

Read JSon file get that data.

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

茶底世界 2024-10-15 18:17:58
   -(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 ;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文