iphone SDK: как использовать URL для отправки данных обратно? - PullRequest
0 голосов
/ 21 сентября 2010

Я читаю данные plist в LightTableViewController.m

и загружаю данные, подобные этим:

    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 " и« status » обратно в базу данных

, чтобы контролировать, какой свет включать или выключать

И эта часть - мой метод сообщения, он находится в 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 голосов
/ 21 сентября 2010

1) Значения POST разделяются символами GET в виде '&' в URL.

2) В строке «Запрос на членство ...» сообщается, что ваш участник не существует или, по крайней мере, не объявлен в этой области, а предупреждение «NSString может не отвечать ...» сообщает, что вы попытка вызвать сообщение / метод для NSString, который должен быть вызван для другого класса (здесь я бы предположил NSDictionary).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...