2016-05-02 2 views
-1

Je suis nouveau à i IOS besoin de savoir comment passer NSString comme paramètre dans nsurlconnection méthode POST, je suis passé chaîne, mais il devient videcomment passer la chaîne en tant que paramètre dans la méthode post

viewdidload:

NSString *parseURL [email protected]"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"]; 
     } 

méthode de publication: Dans la méthode post-i a passé la chaîne en tant que paramètre

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

vous n'êtes pas passer la variable '' str' à votre sendDataToServer: 'méthode, évidemment' 'str' est nil' . – dirkgroten

+0

S'il vous plaît Voir ce lien: - http://stackoverflow.com/questions/36905484/ios-sending-post-method-data-can-access-in-php-as-get-method/36905691#36905691 – Rajesh

Répondre

0

y ous pouvez faire comme

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]; 

// créer la méthode comme

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


NSString *post = [NSString stringWithFormat:@"branch_id=%@",str]; 

.... continue your work 

} 
+0

son montrant pas interface visible pour sendDataToServer @ Anbu.Karthik –

+0

pouvez-vous mettre à jour votre question une fois –

+0

travaille maintenant @ Anbu.Karthik –