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

Three parameter in Post method using nsurlconnection

我是新手 IOS 我需要在 POST 中传递三个参数 method.my 参数是 (1)str (2)str1 (3)str2.this 三个参数以字符串格式从不同的 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];
    }
}

查看加载: 这里我也想要 str1 和 str2 参数。

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

与其将其作为 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];
        }
    }
}

您可以通过多种方式实施

选择-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中的一些代码或者else

  // 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