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

4 回答 4

1
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) {

}];
于 2016-03-14T16:08:30.557 回答
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);
}];
于 2016-03-13T15:22:40.060 回答
0

您可以在此处查看官方迁移指南

于 2016-03-15T08:28:29.467 回答
0

使用通用NSObject类调用 WS 与AFNetworking 3.0 这是我的重复答案,但它已更新为 AFNetworking 3.0 首先在这里使用任何名称创建类NSObject我正在创建名称为Webservice.hWebservice.m的类NSObject

网络服务.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

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

像这样使用从任何viewcontroller.m调用这个通用方法 和从 any 调用任何方法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.0我对 AFNetworking 2.0 的旧回答

于 2016-03-15T11:14:06.750 回答