如何在 post 方法中将字符串作为参数传递
how to pass string as parameter in post method
我是 IOS 的新手,我需要知道如何在 nsurlconnection
POST
方法中将 NSString
作为参数传递,我传递了字符串,但它变成了空
viewdidload
:
NSString *parseURL =@"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"];
}
post方法:
在 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];
}
}
你可以这样做
arrMsg = [json1 valueForKeyPath:@"Branches.branch_name"];
arrmsg1 =[json1 valueForKeyPath:@"Branches.id"];
NSString *str = [arrmsg1 componentsJoinedByString:@","];
NSLog(@"%@",str);
// call the method like
[self sendDataToServer :@"POST" params:str];
// 创建类似
的方法
-(void) sendDataToServer : (NSString *) method params:(NSString *)str{
NSString *post = [NSString stringWithFormat:@"branch_id=%@",str];
.... continue your work
}
我是 IOS 的新手,我需要知道如何在 nsurlconnection
POST
方法中将 NSString
作为参数传递,我传递了字符串,但它变成了空
viewdidload
:
NSString *parseURL =@"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"];
}
post方法: 在 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];
}
}
你可以这样做
arrMsg = [json1 valueForKeyPath:@"Branches.branch_name"];
arrmsg1 =[json1 valueForKeyPath:@"Branches.id"];
NSString *str = [arrmsg1 componentsJoinedByString:@","];
NSLog(@"%@",str);
// call the method like
[self sendDataToServer :@"POST" params:str];
// 创建类似
的方法 -(void) sendDataToServer : (NSString *) method params:(NSString *)str{
NSString *post = [NSString stringWithFormat:@"branch_id=%@",str];
.... continue your work
}