-1

I am new to IOS i need to know how to pass NSString as parameter in nsurlconnection POST method,i passed string but it become empty

viewdidload:

 NSString *parseURL =@"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 method: In post method i passed the string as 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];
    }
}
2
  • 1
    you're not passing the variable str to your sendDataToServer: method, so obviously str is nil. Commented May 2, 2016 at 11:16
  • Please See This Link :- stackoverflow.com/questions/36905484/… Commented May 2, 2016 at 12:25

1 Answer 1

0

you can do like

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

// create the method like

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


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

.... continue your work

}
Sign up to request clarification or add additional context in comments.

1 Comment

its showing no visible interface for sendDataToServer @Anbu.Karthik

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.