Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send json data in the Http request using NSURLRequest
    primarykey
    data
    text
    <p>I'm new to objective-c and I'm starting to put a great deal of effort into request/response as of recent. I have a working example that can call a url (via http GET) and parse the json returned.</p> <p>The working example of this is below</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData: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) } - (void)viewDidLoad { [self searchForStuff:@"iPhone"]; } -(void)searchForStuff:(NSString *)text { responseData = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } </code></pre> <p>My first question is - will this approach scale up? Or is this not async (meaning I block the UI thread while the app is waiting for the response)</p> <p>My second question is - how might I modify the request part of this to do a POST instead of GET? Is it simply to modify the HttpMethod like so?</p> <pre><code>[request setHTTPMethod:@"POST"]; </code></pre> <p>And finally - how do I add a set of json data to this post as a simple string (for example)</p> <pre><code>{ "magic":{ "real":true }, "options":{ "happy":true, "joy":true, "joy2":true }, "key":"123" } </code></pre> <p>Thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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