Я новичок в IOS мне нужно знать, как передать NSString
в качестве параметра в nsurlconnection
POST
метод, я прошел строку, но он пусткак передать строку в качестве параметра в методе пост
viewdidload
:
NSString *parseURL [email protected]"http:url";
NSString *encodeurl =[parseURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodeurl];
NSData *data = [NSData dataWithContentsOfURL:url];
if(data){
NSError *error;
NSDictionary *json1 = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
arrMsg = [json1 valueForKeyPath:@"Branches.branch_name"];
arrmsg1 =[json1 valueForKeyPath:@"Branches.id"];
NSString *str = [arrmsg1 componentsJoinedByString:@","];
NSLog(@"%@",str);
[self sendDataToServer :@"POST"];
}
метод пост: В методе пост я передал строку в качестве параметра
-(void) sendDataToServer : (NSString *) method{
//NSString *str = [arrmsg1 componentsJoinedByString:@","];
NSString *post = [NSString stringWithFormat:@"branch_id=%@",str];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if(theConnection){
// indicator.hidden = NO;
mutableData = [[NSMutableData alloc]init];
}
}
вы не передаете переменную 'str' к вашему' 'sendDataToServer: метод, поэтому очевидно,' 'str' является nil' , – dirkgroten
См. Эту ссылку: - http://stackoverflow.com/questions/36905484/ios-sending-post-method-data-can-access-in-php-as-get-method/36905691#36905691 – Rajesh