0

I am working in an app in which i need to pass the json object in that parameter of the request string, now I am stuck here and have no idea how to do this.

 SBJSON *json = [SBJSON new];
 json.humanReadable = YES;
 responseData = [NSMutableData data] ;

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register";

NSString *requestString = [NSString stringWithFormat:@"{\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"}",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text];


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

NSString *urlLoc=@"";
urlLoc = [urlLoc stringByAppendingString:service];

NSLog(@"URL:- %@",urlLoc);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
                                [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@",request);
Purva
  • 1,291
  • 2
  • 15
  • 30

1 Answers1

1
SBJSON *json = [SBJSON new];
 json.humanReadable = YES;
 responseData = [NSMutableData data] ;

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register";

NSString *requestString = [NSString stringWithFormat:@"{\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"}",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text];


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

NSString *urlLoc=@"";
urlLoc = [urlLoc stringByAppendingString:service];

NSLog(@"URL:- %@",urlLoc);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
                                [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@",request);

Delegate method of Connection

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
  //**check here for responseData & also data**
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
  //do something with the json that comes back ... (the fun part)
}

/ /// / ///////// EDITED Answer /////////////////////

GET Method: In this method you can append the request data behind the web- service.As you doing now by line [request setHTTPMethod: @"POST"];.

POST Method: In this method, you can't append the requested data. But pass the dictionary as a parameter. Like below:

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
  [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];

NSString *jsonRequest = [jsonDict JSONRepresentation];

NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
Rohan
  • 2,939
  • 5
  • 36
  • 65
  • when i make a GET request it gives me correct response but when i try to post there is no problem but it gives me an empty array instead of correct value – Purva Jul 10 '13 at 12:38
  • yes when i pass the login data it is not giving me requires output ,is there any thing i need to chenge in making request passing json string – Purva Jul 10 '13 at 12:55
  • check this link: [http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service] – Rohan Jul 12 '13 at 04:50
  • Try this one also [http://stackoverflow.com/questions/13359314/json-post-is-not-working-in-ios]. – Rohan Jul 12 '13 at 04:56
  • This will nt wok I have used ASI HTTP request for passing json objects in PHP webservices – Purva Jul 12 '13 at 07:37