【问题标题】:iPhone + Drupal + JSON RPC Server problemiPhone + Drupal + JSON RPC 服务器问题
【发布时间】:2011-02-05 11:34:20
【问题描述】:

我不知道如何使用 Obj-C 发布 JSON RPC 请求。谁能帮帮我?

到目前为止我有:

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 服务器和 JSON rpc 服务器。

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

请帮帮我。

【问题讨论】:

    标签: iphone objective-c json drupal rpc


    【解决方案1】:

    这解决了它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 2013-11-24
      • 2013-07-01
      • 1970-01-01
      相关资源
      最近更新 更多