iPhone + Drupal + JSON RPC 服务器问题 [英] iPhone + Drupal + JSON RPC Server problem

查看:27
本文介绍了iPhone + Drupal + JSON RPC 服务器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用 Obj-C 发布 JSON RPC 请求.有人可以帮我吗?

I don't have any idea how to post a JSON RPC request using Obj-C. Can anyone help me?

到目前为止我有:

responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://*************/services/json-rpc"]];

NSString *jsonString = @"{"jsonrpc": "2.0","method": "node.get", "params": { "arg1": 1 } ,"id": "dsadasdas"}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];

[ request setHTTPMethod: @"POST" ];
[ request setHTTPBody: jsonData ];
[ request setValue:@"application/json" forHTTPHeaderField:@"Content-> Type"];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

我正在使用 drupal + services + Json Server &JSON RPC 服务器.

I'm using drupal + services + Json Server & JSON rpc server.

似乎我用第一个得到了更好的结果,问题是我认为构建帖子的主体...

Seems I'm getting better results with the first one, the problem is building the body of the post i Think...

请帮帮我.

推荐答案

修复了它:

SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = @"node.get";

NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"1",@"nid",
                               nil];
//Pass it twice to escape quotes
NSString *jsonString = [NSString stringWithFormat:@"%@", [params JSONFragment], nil];
NSString *changeJSON = [NSString stringWithFormat:@"%@", [jsonString JSONFragment], nil];

NSLog(jsonString);
NSLog(changeJSON);


NSString *requestString = [NSString stringWithFormat:@"method=node.get&vid=1",service,changeJSON,nil];
NSLog(requestString);


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


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://******************/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);

这篇关于iPhone + Drupal + JSON RPC 服务器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆