iPhone + Drupal + JSON RPC Server problem

后端 未结 1 1482
梦谈多话
梦谈多话 2021-02-06 18:43

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

So far I have:

responseData = [[NSMutableData data] retain];
NSMut         


        
相关标签:
1条回答
  • 2021-02-06 19:16

    This fixed it:

    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);
    
    0 讨论(0)
提交回复
热议问题