2016-05-02 18 views
-1

Ich bin neu in IOS Ich muss wissen, wie in nsurlconnectionPOST Methode NSString als Parameter zu übergeben, übergeben i Zeichenfolge aber eswie String, der als Parameter in der Post-Methode übergeben

viewdidload leer geworden:

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

post-Methode: In Post-Methode übergeben i die Zeichenfolge als Parameter

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

Sie nicht die Variable 'str' zu' sendDataToServer vorbei: 'Methode, so offensichtlich' str' ist 'nil' . – dirkgroten

+0

Bitte sehen Sie diesen Link: - http://stackoverflow.com/questions/36905484/ios-sending-post-method-data-can-access-in-php-as-get-method/36905691#36905691 – Rajesh

Antwort

0

y ou tun können, wie

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

// die Methode erstellen wie

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


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

.... continue your work 

} 
+0

seine Anzeige nein sichtbare Schnittstelle für sendDataToServer @ Anbu.Karthik –

+0

können Sie Ihre Frage einmal –

+0

jetzt arbeiten @ Anbu.Karthik aktualisieren –

Verwandte Themen