使用nsurlconnection的Post方法中的三个参数

时间:2016-05-09 04:24:17

标签: ios objective-c http-post nsurlconnection url-parameters

我是IOS的新手我需要在POST方法中传递三个参数。我的参数是(1)str(2)str1(3)str2.t这三个参数是以字符串格式从不同的URL获取。

编写POST方法:   我需要在方法中添加这些参数吗?我已经添加了str参数但我正在努力传递其他两个(str1,str2)参数。

-(void) sendDataToServer : (NSString *) method params:(NSString *)str{

    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 ){

        mutableData = [[NSMutableData alloc]init];
    }
}

viewDidLoad中: 这里我也想要str1和str2参数。

[self sendDataToServer :@"POST" params:str];

2 个答案:

答案 0 :(得分:1)

您可以通过多种方式实施

<强>选择-1

-(void) sendDataToServer : (NSString *) method firstparams:(NSString *)firststr secondparam:(NSString *)secondstr thirdparam:(NSString *)thirdstr{

 NSString *post = [NSString stringWithFormat:@"branch_id=%@&xxxx=%@&yyyyy=%@",firststr,secondstr,thirdstr];

// continue your works as its same flow

调用方法如

[self sendDataToServer :@"POST" firstparams:@"yourbranchID" secondparam:@"xxxValue" thirdparam:@"yyyyvalue"];

<强>选择-2

你所做的是正确的,只需修改viewdidload中的一些代码,否则

  // add all values in one string using  stringWithFormat
  NSString *str = [NSString stringWithFormat:@"branch_id=%@&xxxx=%@&yyyyy=%@",firststr,secondstr,thirdstr];
// and pass the param to web call
[self sendDataToServer :@"POST" params:str];

调用方法为

-(void) sendDataToServer : (NSString *) method params:(NSString *)str{
// no need of this line
// NSString *post = [NSString stringWithFormat:@"branch_id=%@",str];

// directly called the str in her
NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

 /.... as its is continue the same work

答案 1 :(得分:-2)

而不是将其作为NSString传递,只需在全局声明的数组中添加三个字符串,并向其添加对象并将其发送到Web服务。 或者将它们创建为NSDictionary并将它们转换为json字符串到Web服务。

NSDictionary *params = @{@"param1": str1, @"param2": str2, @"param3": str3 };
[self sendDataToServer :@"POST" params:params];

-(void) sendDataToServer : (NSString *) method params:(NSDictionary *)dict
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];

    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];


        [request setHTTPMethod:@"POST"];
        [request setValue:jsonString forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:jsonData];

        NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

        if( theConnection ){

            mutableData = [[NSMutableData alloc]init];
        }
    }
}