iphone SDK:如何使用URL发回数据?

时间:2010-09-21 10:09:11

标签: iphone url post sdk

我在LightTableViewController.m

读取了一个plist数据

我加载了这样的数据:

    LOG RESULT:   
The Plist Data Is : (
        {
        category = 11;
        id = 1;
        name = "LIVING RM";
        status = 0;
    },
        {
        category = 11;
        id = 2;
        name = "BEDROOM RM";
        status = 0;
    }
)

我需要将“ ID ”和“状态”发回数据库

控制哪个灯打开或关闭

这部分是我的帖子方法,它在LightCell0.m

- (void)switchToggled:(id)sender {

    UISwitch *theSwitch = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)theSwitch.superview.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];



    if(theSwitch.on) {
        NSURL * url;
        NSMutableURLRequest * request;  
        NSString *_urlString = @"http://10.85.28.99/req_light.php"; 
        url = [self smartURLForString:_urlString];
        request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];


            //This is my first post method,it is static,only get the indexPath,Not a real id and status I want to post back 
        NSString *post = [NSString stringWithFormat:@"lightcb%i=1", indexPath.row+1];
        NSData *postData = [ NSData dataWithBytes: [ post UTF8String ] length: [ post length ] ];
        [request setHTTPBody:postData];
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
    } 
    else {
        NSURL * url;
        NSMutableURLRequest * request;
        NSString *_urlString = @"http://10.85.28.99/req_light.php";
        url = [self smartURLForString:_urlString];
        request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];


            //I got one error and one warring 
        //Error is "Request for member 'plist' in something not a structure or union"
            //Warning is 'NSString' may not resopnd to '+stringWithFormat:value=ForKey'
        NSString *post = [ NSString stringWithFormat:@"id=%i,status=0", 
                                  [[self.plist objectAtIndex:indexPath.row] valueForKey:@"id"]];
        NSData *postData = [ NSData dataWithBytes: [ post UTF8String ] length: [ post length ] ];
        [request setHTTPBody:postData];
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

    }
}

所以...我的问题是

< 1>如何发回两个数据(是否使用“,”分隔两个返回变量?

< 2>如何消除错误“请求成员'plist'在非结构或联合的东西中”

非常感谢

1 个答案:

答案 0 :(得分:0)

1)POST值由'&'分隔比如URL中的GET值。

2)'请求成员'...行告诉您您的成员不存在,或者至少未在此范围内声明,警告'NSString可能不响应......'告诉您试图在NSString上调用一个应该在另一个类上调用的消息/方法(NSDictionary就是我的猜测)。