首页 文章

如何在AFNetworking 3.0中迁移?

提问于
浏览
3

我在AFNetworking 3.0中迁移 . 我在AFNetworking中使用AFHTTPRequestOperation但它在最近的更新中被删除了 . 我尝试了所有可能的解决方案 . 基本上我需要发布带有Header的JSON . 我将我的NSDIctionary转换为JSON对象,然后将其添加为字符串 . 这是带有Header的示例JSON

RequestHeader={
  "lname" : "sadhsdf",
  "bday" : "2003-03-13",
  "age" : "12",
  "address" : "dsfhskdjgds",
  "gender" : "M",
  "cnumber" : "12312412",
  "fname" : "sadhsdf",
  "RequestType" : "userregistration",
  "Email" : "sldjlkasd@sjdhflksdf.com",
  "status" : "dsfhskdjgds",
  "Password" : "123456"
}

RequestHeader是一个NSString,其余的是NSDictionary .

如何在AFNetworking 3.0中应用它?先感谢您 .

4 回答

  • 0
    NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:URL sessionConfiguration:configuration];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setValue:@"some value" forKey:@"someKey"];
    
    [manager POST:@"search" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //If you need to send image
        UIImage *image = [UIImage imageNamed:@"my_image.jpg"];
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:@"Image" fileName:@"my_image.jpg" mimeType:@"image/jpeg"];
    
    } success:^(NSURLSessionDataTask *task, id responseObject) {
    
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    
    }];
    
  • 0

    你可以查看官方移民指南here

  • 0

    使用常见的 NSObject 类来调用WS与 AFNetworking 3.0 这是我的重复答案,但它是 Updated with AFNetworking 3.0 首先使 NSObject 具有任何名称的类我在这里创建 NSObject 类名为 Webservice.hWebservice.m

    Webservice.h

    @interface Webservice : NSObject
    
    +  (void)requestPostUrl:(NSString *)strURL parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure;
    
    @end
    

    Webservice.m 你的nsobject.m文件看起来像这样 . (在.m文件中添加两个函数)

    #import "Webservice.h"
    
    #define kDefaultErrorCode 12345
    
    @implementation Webservice
    
    +  (void)requestPostUrl:(NSString *)strURL parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    
        [manager POST:strURL parameters:dictParams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if([responseObject isKindOfClass:[NSDictionary class]]) {
                if(success) {
                    success(responseObject);
                }
            }
            else {
                NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                if(success) {
                    success(response);
                }
            }
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if(failure) {
                failure(error);
            }
    
        }];
    }
    
    @end
    

    确保您必须替换成功的字典键和处理响应回调函数的消息

    Use like this 从任何 viewcontroller.m 和任何 viewControllers 中的任何方法调用此常用方法 . 暂时我使用 viewDidLoad 来调用这个WS .

    - (void)viewDidLoad {
            [super viewDidLoad];
    
            NSDictionary *dictParam = @{@"parameter1":@"value1",@"parameter1":@"value2"};
    
            [Webservice requestPostUrl:@"add your webservice URL here" parameters:dictParam success:^(NSDictionary *responce) {
                //Success
                NSLog(@"responce:%@",responce);
                //do code here
            } failure:^(NSError *error) {
                //error
            }];
        }
    

    在上面的方法中添加您的参数,值和Web服务URL . 你可以轻松使用这个 NSObjcet 类 . 有关详细信息,请访问AFNetworking 3.0my old answear with AFNetworking 2.0 .

  • 0
    NSURL *url = [NSURL URLWithString:@"https://example.com/"];
    
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
    
                            height, @"user[height]",
    
                            weight, @"user[weight]",
    
                            nil];
    
    [httpClient postPath:@"/myobject" parameters:params 
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
        NSLog(@"Request Successful, response '%@'", responseStr);
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
    }];
    

相关问题