简体   繁体   中英

NSURLConnection Response

Hi I have this code.

NSString *urlToAuthPage = [[NSString alloc] initWithFormat:@"&name=%@&street=%@&city=%@&state=%@&zip=%@&lat=%@&lon=%@&hash=%@", name, street, city, state, zip, str1, str2, hash];

        NSData *postData = [urlToAuthPage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%d",[urlToAuthPage length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://oo.mu/partyapp/post-party.php"]]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];        


        NSString *infoString = [NSString stringWithFormat:@"http://oo.mu/partyapp/post-party.php?name=%@&street=%@&city=%@&state=%@&zip=%@&lat=%@&lon=%@&hash=%@", name, street, city, state, zip, str1, str2, hash];
        NSURL *infoUrl = [NSURL URLWithString:infoString];
        NSData *infoData = [NSData dataWithContentsOfURL:infoUrl];
        NSError *error;
        responseDict = [NSJSONSerialization JSONObjectWithData:infoData options:0 error:&error];
        NSLog(@"%@", responseDict);

You probably noticed that I have code that I don't need which I'm aware of but I'm getting the wrong response for something else. How can I clean some of the code up and get the response from request which is with urlToAuthPage?

I think I see what you are saying: you know how to invoke the request using the NSData method and a URL, but this doesn't support POST where you want to supply a request body.

The other issue is, even if you get this to work, it would be synchronous. There's a nice solution in NSURLConnection class. Build your request as you do in the first half of your code (with the post data in the body). Then do this:

[NSURLConnection sendAsynchronousRequest:request
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    // your data or an error will be ready here
}];

As highlighted by @danh above, you can choose between synchronous and asynchronous requests with NSURLConnection. I wanted to mention that you can achieve async with delegation instead of blocks, if that seems more approachable to you.

Basically, you can implement the methods in NSURLConnectionDelegate and NSURLConnectionDataDelegate, and then assign yourself as the delegate of the connection and respond appropriately during the callbacks defined in those protocols.

[NSURLConnection connectionWithRequest:yourNSMutableURLRequest delegate:self];

If you are doing this often, you can do like I did, and abstract all of the asynchronous stuff into a connection manager, which can handle synchronous and asynchronous requests (for async, it stores a pointer back to the interested party and executes a callback when the request finishes). It's a simple interface that I use all over the place for both kinds of requests, and it plays well with further abstractions (like making a webRequest class to encapsulate a POST request with a body).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM