2012-01-15 65 views
0

我确定我很喜欢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]; 

虽然,我还不清楚为什么这些属性没有设置。但是,看起来我至少有数据。

回答

3

创建请求操作对象不会自动启动请求。就像您在编辑中指出的那样,您可以将其排入NSOperationQueue或手动执行[operation start]

我建议您使用AFHTTPClient与您的服务器通信,因为它提供了一个内置机制,可以根据您传入的参数正确设置查询字符串或HTTP正文。请参阅示例中的Gowalla API示例客户端代码和方法AFHTTPClient -postPath:parameters:success:failure

+0

感谢您的回复。 – user1146403 2012-01-18 02:13:14

相关问题