AFNetworking 2.0
我是iOS开发新手,我想弄清楚如何发送GET请求AFNetworking。AFNetworking 2.0
我以前在Tutorial on Using AFNetworking 2.0提供的例子,并把它放在主期待一个错误:
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);
}];
...和我没有什么,所以我试图指向返回一个有效的JSON的WebService也一无所获。为什么没有块(成功或失败)被执行?
您的代码不会执行块(成功或失败),因为出错了。 但如果你使用try catch块,那么它会进入catch块尝试一次。 代码:
尝试{
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**
}
刚刚做了,没有人提出。这很奇怪。我只是成功执行代码。 – Arthur 2015-03-02 13:13:25
使用以下function
,只是发送您的URL
在Argument
- (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];
}
OP询问AFNetworking 2.0不是1.x – Andrea 2015-03-02 09:18:47
很奇怪,我刚刚创建另一个项目(iOS作业系统,而不是OSX)并从IBAction调用相同的代码,并且它很容易工作。在Podfile中,我指定了新版本的AFNetworking(2.5而不是2.4)。我不知道这是否相关。 谢谢大家试图帮助我。
请尝试下面的代码来做到这一点。
@try {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:stringURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
@catch (NSException *exception) {
NSLog(@"Exception - %@", [exception debugDescription]);
}
@finally {
}
并确保您有一个有效的JSON。
你有没有在'viewdidload'或按钮动作的某个地方调用这个方法? 如果是的话, 你的错误是什么? – magid 2015-03-02 06:36:27
不要把它放在主要功能中。 – AdamPro13 2015-03-02 08:01:28
你确定你不需要任何参数吗? – Andrea 2015-03-02 09:20:34