如何通过AFNetworking进行GET请求? [英] How to do GET request via AFNetworking?
问题描述
我向项目经理添加了目录 AFNetworking
和 UIKit + AFNetworking
。
我使用最新的库3.0.4
I added directories AFNetworking
and UIKit+AFNetworking
to project manager.
I use latest library 3.0.4
在我的类文件中导入文件 AFNetworking.h
之后的.m
。
我找到了很多如何向url发出请求的示例,但是信息已经过时了。
After I imported file AFNetworking.h
in my class file .m
.
I found a lot examples how to make request to url, but information is old.
如何对URL进行GET请求并从服务器获取数据?
现在函数 AFHTTPRequestOperation
已被删除。
How to make GET request to URL and get data from server?
Now function AFHTTPRequestOperation
was removed.
这是第一次使用xcode中的库,我是初学者。
This is first time working with libraries in xcode, I am beginner.
推荐答案
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://google.com/"]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
path:@"http://google.com/api/pigs/"
parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
===================== ========================
=============================================
您还可以使用AFHTTPRequestOperation:
You can also use AFHTTPRequestOperation:
NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
networkQueue.maxConcurrentOperationCount = 5;
NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
}];
[networkQueue addOperation:operation];
这篇关于如何通过AFNetworking进行GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!