Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For JSON posts in my app I use SBJson. This method uses an NSURLConnection along with a HTTP Post request. The return data, when parsed, will come in the form of keys and strings in an NSDictionary. (jsonData is an NSDictionary established somewhere elese) (Apparently NSJSONSerialization does the same thing though)</p> <pre><code>(void) doJson:(NSString *)post { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, FALSE); NSURL *url=[NSURL URLWithString:@"youraddress"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; // [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; NSError *error = [[NSError alloc] init]; NSHTTPURLResponse *response = nil; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; SBJsonParser *jsonParser = [SBJsonParser new]; jsonData = [jsonParser objectWithString:responseData error:nil]; NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload