我确定我很喜欢iOS编程的新手,但是我遇到了AFNetworking的问题。具体来说,当我使用它时,没有任何反应。我没有从服务器得到任何回应,但是,我也没有得到任何错误。所有的属性都以NULL结尾。AFNetworking默默无闻
这里是我的要求:
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]];
这似乎是工作,因为我得到我预期的结果,如果我只是请求传递到一个UIWebView,或者如果我用这个:
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"responseData: %@", content);
在其他单词,POST成功并返回JSON。
但是,这些都不做任何事情(没有错误,没有反应):
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Data: %@ %@", [JSON valueForKeyPath:@"firstName"], [JSON valueForKeyPath:@"lastName"]);
} failure:nil];
也不即使是裸露的骨头尝试的工作:
AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
很显然,我可怕做的事情错误。但是,沉默的失败使得这很难弄清楚。
感谢您的任何帮助。
编辑:
没关系..我很密集。
没有创建NSOperationQueue。
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
虽然,我还不清楚为什么这些属性没有设置。但是,看起来我至少有数据。
感谢您的回复。 – user1146403 2012-01-18 02:13:14