我向項目經理添加了目錄AFNetworking
和UIKit+AFNetworking
。 我使用最新庫3.0.4如何通過AFNetworking做GET請求?
我導入文件AFNetworking.h
在我的課文件.m
。 我發現了很多示例如何向url發送請求,但信息已老化。
如何讓GET請求到URL並從服務器獲取數據? 現在函數AFHTTPRequestOperation
已被刪除。
這是第一次在xcode中使用庫,我是初學者。
我向項目經理添加了目錄AFNetworking
和UIKit+AFNetworking
。 我使用最新庫3.0.4如何通過AFNetworking做GET請求?
我導入文件AFNetworking.h
在我的課文件.m
。 我發現了很多示例如何向url發送請求,但信息已老化。
如何讓GET請求到URL並從服務器獲取數據? 現在函數AFHTTPRequestOperation
已被刪除。
這是第一次在xcode中使用庫,我是初學者。
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:
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 3.0你需要請求作爲下:
NSURL * urlStr = [NSURL URLWithString:strURL];
NSDictionary *dictParameters = @{@"user[height]": height,@"user[weight]": weight};
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager GET:url.absoluteString parameters:dictParameters success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"PLIST: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
對於AFNetworking 3.X:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
作爲提問者談論AFNetworking 3.0+的最佳答案。 AFNetworking 3.0不再支持上述其他解決方案。 – AndaluZ
同意,upvote的知名度 – Multinerd
這應該是接受的答案! – armnotstrong
(void)postDatabyAF{
NSString *url;
NSDictionary *dict;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:
NSJSONReadingAllowFragments];
[manager POST:url parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@" operation.responseData %@",operation.responseData);
NSLog(@" operation.response %@",operation.response);
NSLog(@"statusCode %ld",operation.response.statusCode);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
if (error) {
}
else{
}
}];
}
please try below answer.
+(void)callAFWS:(NSDictionary *)dict withURL:(NSString *)strUrl
withToken:(NSString *)strToken withBlock:(dictionary)block
{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[manager.requestSerializer setValue:strToken forHTTPHeaderField:@"Authorization"];
[manager GET:[NSString stringWithFormat:@"%@/%@",WebserviceUrl,strUrl] parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (!responseObject)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:ServerResponceError forKey:@"error"];
block(responseObject);
return ;
}
block(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:ServerResponceError forKey:@"error"];
block(dict);
}];
}
你不需要爲第三方庫,檢查NSURLSession。 –