0

I am new to IOS i created nsurlconnection using POST method i passed a parameter "str".In nslog str value become null how to pass my string "str" in post method.

str coding: str string contain branchid1,currencyid1,currencyid2 are taken from picker view did select row.

str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",branchid1,currencyid1,currencyid2,fourthstr];

Picker view coding:

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    if(pickerView.tag ==2){
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];
        branchid1 = (NSString *)[arrmsg1 objectAtIndex:row];

        NSLog([arrmsg1 objectAtIndex:row]);

    }else if(pickerView.tag ==1){
    currency1.text = (NSString *)[currencyname1 objectAtIndex:row];
        currencyid1 = (NSString *)[id1 objectAtIndex:row];
        NSLog([id1 objectAtIndex:row]);


    }
    else
    {
        currency2.text = (NSString *)[from_currency objectAtIndex:row];
        currencyid2 = (NSString *)[id2 objectAtIndex:row];

        NSLog(@"%@",currencyid2);
        NSLog([id2 objectAtIndex:row]);
    }

button action:

}

post method:

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


    NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
    NSLog(@"%@",str);

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

button action:

- (IBAction)btnact:(id)sender {

        if (branchides.length!=0 && currencyide1.length!=0 && currencyid2.length!=0 && fourthstr.length!=0)
    {
        str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",branchides,currencyide1,currencyid2,fourthstr];
        NSLog(@"%@",str);

        //[self sendDataToServer :@"POST" params:str];
    }
    else
    {
        // show some alert for empty
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value"
                                                    message:final
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"ok", nil];

    alert.tag = 100;
    [alert show];
    [self sendDataToServer :@"POST" params:str];
}
A.sonu
  • 159
  • 10
  • check this link http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios – sohil May 10 '16 at 09:41
  • where you called this method -(void) sendDataToServer : (NSString *) method params:(NSString *)str – Anbu.Karthik May 10 '16 at 09:43
  • at the same time where you declared this line str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",branchid1,currencyid1,currencyid2,fourthstr]; – Anbu.Karthik May 10 '16 at 09:44
  • i try to help you last 5 questions but you could not give the proper information , then how do you get the answer – Anbu.Karthik May 10 '16 at 09:47
  • -(void) sendDataToServer : (NSString *) method params:(NSString *)str – this method i called in viewdidload @Anbu.Karthik – A.sonu May 10 '16 at 09:48
  • str = [NSString stringWithFormat:@"branch_id=%@&from_curr=%@&to_curr=%@&value=%@",branchid1,curr‌​encyid1,currencyid2,fourthstr]; declared this line in button action @Anbu.Karthik – A.sonu May 10 '16 at 09:49
  • 200 % is not work , I told called in button action after that it will work , the reason in view didload , you dont have any values on variable – Anbu.Karthik May 10 '16 at 09:49
  • can you show that button action and view didload once – Anbu.Karthik May 10 '16 at 09:50
  • ya i changed in button action also but no change@Anbu.Karthik – A.sonu May 10 '16 at 09:55
  • can you update the question with entire class code – Anbu.Karthik May 10 '16 at 10:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111510/discussion-between-a-sonu-and-anbu-karthik). – A.sonu May 10 '16 at 10:02
  • Actually the top-answer to [this question](http://stackoverflow.com/questions/21299362/is-there-any-way-to-attach-a-nsdictionary-of-parameters-to-an-nsurlrequest-inste) is a better approach. – trojanfoe May 10 '16 at 10:15
  • try this http://toranbillups.com/blog/archive/2011/04/10/Submit-data-using-an-http-post-in-objective-c/ – sohil May 10 '16 at 10:18
  • Your length is wrong. It should be the length of postData (the encoded blob), not the length of the original string (the number of characters). At best, you'll get truncated values. Also, you need to URL-encode the form data fields. For details, see https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/WorkingwithURLEncoding/WorkingwithURLEncoding.html – dgatwood May 18 '16 at 20:50
  • Also, str is only non-nil if the if statement evaluates to true. So most of the time, you're calling it with nil. – dgatwood May 18 '16 at 20:57

0 Answers0