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

问题描述:

我是新来的IOS我需要在POST method.my参数中传递三个参数(1)str(2)str1(3)str2.this这三个参数是从不同的url字符串格式。Post方法中使用nsurlconnection的三个参数

编码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]; 
+0

你为什么不使用数组? –

+0

三个参数取自不同的url,所以我转换成不同的字符串,如果它可能转换为数组?@TysonVignesh –

+0

你在哪里调用了这个方法,[self sendDataToServer:@“POST”params:str]; –

您可以通过多种方式实现

选择-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 
+0

如果你碰到我肯定会解释我的代码 –

+0

按照我遵循我的代码的方式。 –

+0

负选民可以解释一次什么错误,你在这里找到 –

,而不是把它当作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]; 
     } 
    } 
} 
+0

请给我任何细节编码我是新来的ios @Tyson Vignesh –

+0

它取决于Web服务开发人员说它作为单个字符串或json字符串 –

+0

你检查了我的更新答案或其他答案还是足够的吗? @ A.sonu –