Send JSON data using NSURLConnection in Xcode

I am trying to send JSON data to a web server through the code below. For some reason, the request does not seem to be coming out. What does it look like, how did I disappear? Also the result of NSURLConnection (retStr) is always empty?

NSDictionary *data = [NSDictionary dictionaryWithObject:@"test sending ios" forKey:@"value1"]; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:kNilOptions error:&error]; NSURL *url = [NSURL URLWithString:@"http://webserveraddress"]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:nil timeoutInterval:60]; [req setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [req setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; [req setHTTPMethod:@"POST"]; [req setHTTPBody:jsonData]; NSString *retStr = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil] encoding:NSUTF8StringEncoding]; 
+4
source share
3 answers

To send simple data in post vars to your php web server you just do it in

Example

 NSString * key = [NSString stringWithFormat:@"var1=%@&var2=%@&var3=%@",@"var1String" ,@"var2string" ,[NSnumber numberWithBool:YES]]; NSURL * url = [NSURL URLWithString:@"http://webserver.com/yourScriptPHP.php"]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[key dataUsingEncoding:NSUTF8StringEncoding]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; // this is for you to be able to get your server answer. // you will need to make your class a delegate of NSURLConnectionDelegate and NSURLConnectionDataDelegate myClassPointerData = [[NSMutableData data] retain]; 

Run

 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [myClassPointerData appendData:data] } -(void)connection:(NSURLConnection *)connection DidFinishLoading { // do what you want with myClassPointerData the data that your server did send you back here // for info on your server PHP  you just need to do: echo json_encode(array('var1'=> $var1, 'var2'=>$var2...)); // to get your server sending an answer } 
+3
source

after that check

ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: @ "ur url"]];

 [request setPostValue:appdelegate.userid forKey:@"userid"]; [request setPostValue:self.nameLbl.text forKey:@"username"]; [request setPostValue:self.website.text forKey:@"website"]; NSLog(@"~~~~~~ request~~~~~ %@",request); [request setTimeOutSeconds:5000]; [request startSynchronous]; 
0
source

Instead of sending as JSON, you can simply send it in the usual way - by setting an HTTPBody request using the POST method.

Only the differences you need to make if you need to call, for example 'Http://abc.example.com/user_profile.json?user [first_name] = fdffdf & user [last_name] = dffdf'

you need your dictionary keys to have a prefix. That is, "first_name = fdffdf" must be changed to "user [first_name] = fdffdf".

Try this bit of code to change the parameter dictionary in the required format for JSON ..

 for (id key in [self allKeys]) { NSString *newKey = [NSString stringWithFormat:@"%@[%@]", parent, key]; [result setObject:[self objectForKey:key] forKey:newKey]; } 
0
source

All Articles