开发者

iphone SDK:How use URL to post data back?

I read a plist data at LightTableViewController.m

and I load a data like this :

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

I need to post the "id" and "status" back to the database

to control which light to turn on or turn off

And this part is the my post method,It's in 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开发者_如何学Go length ] ];
        [request setHTTPBody:postData];
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

    }
}

So...my question is

<1>How to post two data back (is it right to use "," to separated two return variables ?

<2>How to eliminate the error "Request for member 'plist' in something not a structure or union"

Great Thanks


1) POST values are separated by '&' like GET values in an URL.

2) The 'Request for member'... line tells you that your member does not exist, or at least is not declared in this scope, the warning 'NSString may not respond...' tells you you're trying to invoke a message/method on NSString which should be invoked on another class (NSDictionary would be my guess here).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜