我是iOS开发方面的新手,正在尝试了解如何使用AFNetworking发送GET请求。
我使用了Tutorial on Using AFNetworking 2.0中提供的示例,并将其放在main中,以期待出现错误:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://samwize.com/api/poos/"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];...and我什么也没有得到,所以我试图指向返回有效JSON的and服务,但也什么也没有得到。为什么不执行任何块(成功或失败)?
发布于 2015-03-02 15:05:20
您的代码不会执行块(成功或失败),因为有些地方出错了。但是如果你使用try catch块,那么它将进入catch块try一次。代码:
尝试{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://samwize.com/api/poos/"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}@catch (NSException *exception) {
**// you will get error here**
}发布于 2015-03-02 15:34:58
使用下面的function,只需在Argument中发送您的URL
- (void)callWebservice : (NSString *)strURL{
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL urlWithEncoding:strURL]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"" parameters:nil];
[request setTimeoutInterval:180];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"Success");
} failure:^ (NSURLRequest *request, NSURLResponse *response, NSError *error, id json){
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"Failure");
}];
[GlobalManager setOperationInstance:operation];
[operation start];
}发布于 2015-03-22 04:09:13
这很奇怪,我只是创建了另一个项目(iOS而不是OSX),并从IBAction调用相同的代码,它很容易工作。在Podfile中,我指定了AFNetworking的新版本(2.5而不是2.4)。我不知道这是否相关。感谢大家对我的帮助。
https://stackoverflow.com/questions/28803846
复制相似问题