web-dev-qa-db-ja.com

AFNetworking経由でGETリクエストを行う方法

ディレクトリAFNetworkingUIKit+AFNetworkingをプロジェクトマネージャーに追加しました。最新のライブラリ3.0.4を使用しています

クラスファイルAFNetworking.hにファイル.mをインポートした後。 URLへのリクエスト方法の例をたくさん見つけましたが、情報が古いです。

URLへのGETリクエストを作成し、サーバーからデータを取得する方法は?関数AFHTTPRequestOperationは削除されました。

Xcodeでライブラリを扱うのはこれが初めてです。私は初心者です。

8
user2565968
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];
1
Akash

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);
}];
79
Chanchal Raj
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);
}];
}
1
herry.master