2016-05-09 19 views
-2

我是新來的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

您可以通過多種方式實現

選擇-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

負選民可以解釋一次什麼錯誤,你在這裏找到 –

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

請給我任何細節編碼我是新來的ios @Tyson Vignesh –

+0

它取決於Web服務開發人員說它作爲單個字符串或json字符串 –

+0

你檢查了我的更新答案或其他答案還是足夠的嗎? @ A.sonu –