0

error in data using encodingI am using two links.First link i created nsaaray for "id".This "id" i need to pass as parameter in nsurlconnection using POST method by second link.i tried lots but i stuck in while passing parameter "id".

nsaaray for id:

- (IBAction)button_drop:(id)sender {
    NSString *parseURL =@"first_link";
    NSString *encodeurl =[parseURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:encodeurl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if(data){
        NSError *error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];

        arrMsg = [json valueForKeyPath:@"Branches.branch_name"];

        arrmsg1 =[json valueForKeyPath:@"Branches.id"];


        [_picker2 reloadAllComponents];

    }


}

POST method using nsurlconnection:

NSString *Branchid=@"3";


    NSURL *url = nil;
    NSMutableURLRequest *request = nil;
    if([method isEqualToString:@"GET"]){

        NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@\n", URL, Branchid];
        url = [NSURL URLWithString: getURL];
        request = [NSMutableURLRequest requestWithURL:url];
        NSLog(@"%@",getURL);

    }else{  // POST

        NSString *parameter = [NSString stringWithFormat:@"branch_id=%@",Branchid];
        NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

        url = [NSURL URLWithString: URL];
        NSLog(@"%@", parameterData);
        request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPBody:parameterData];

        arr= [NSMutableString stringWithUTF8String:[parameterData bytes]];
        }

    [request setHTTPMethod:method];
    [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if( connection )
    {
        mutableData = [NSMutableData new];
    }
A.sonu
  • 159
  • 10

1 Answers1

0

Try to create a "POST" method like this:-

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                            Branchid, @"branch_id", nil];
NSString *newRequest = [dict JSONRepresentation];

NSData *requestData = [newRequest dataUsingEncoding:NSUTF8StringEncoding];

//If you are not using SBJsonParser Library then try the following:
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *urlString = @"YOUR URL String";
NSURL *requestURL = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long) [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (connection) {
    mutableData = [NSMutableData new];
}

Also, You can follow this link if you don't find out the answer working properly:- Data in POST or GET methods

Send data in POST methods

Community
  • 1
  • 1
Nirmit Dagly
  • 1,272
  • 1
  • 12
  • 25