문제

I'm sure I'm dense from being new to iOS programming, but, I am having problems with AFNetworking. Specifically, when I use it, nothing happens. I don't get any response from the server, but, I also don't get any errors. All of the properties end up NULL.

Here is my request:

NSMutableDictionary *requestArray = [NSMutableDictionary new];
[requestArray setObject: @"getBio"
                 forKey: @"action"];
[requestArray setObject: @"1"
                 forKey: @"bioID"];

NSError * error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestArray options:0 error:&error];

NSString *myRequestString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://localhost/LP/JSON/Secure/bio.php" ] ]; 

[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: [myRequestString dataUsingEncoding:NSUTF8StringEncoding]];

This seems to work as I get my expected result if I simply pass the request into a UIWebView or if I use this:

NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"responseData: %@", content);

In other words, the POST is successful and JSON is returned.

But, none of these do anything (no error, no response):

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Data: %@ %@", [JSON valueForKeyPath:@"firstName"], [JSON valueForKeyPath:@"lastName"]);
} failure:nil];

Nor does even the bare bones attempt work:

AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];

Obviously, I'm doing something horribly wrong. But, the silent failures are making this hard to figure out.

Thanks for any help.

EDIT:

Nevermind.. I am dense.

Wasn't creating the NSOperationQueue.

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

Although, I'm still unclear why the properties aren't getting set. But, it looks like I at least have data.

도움이 되었습니까?

해결책

Creating the request operation object does not automatically kick off the request. Like you pointed out in your edit, it's up to you to either enqueue it into an NSOperationQueue or do [operation start] manually.

I would recommend that you communicate with your server using AFHTTPClient, since that provides a built-in mechanism to correctly set either your query string or HTTP body based on parameters you pass in. See the Gowalla API example client in the sample code, and the method AFHTTPClient -postPath:parameters:success:failure.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top