[AFNetworking练习1]Post方式提交json返回json

/*POST请求*/
    NSURL* BaseUrl = [NSURL URLWithString:@"http://10.2.108.10:8086/"];
    /*这里注意,字典的key必须支持KeyType <NSCopying>协议,NSNumber明显不合适~*/
    NSMutableDictionary<NSString*,NSNumber*>* param = [NSMutableDictionary<NSString*,NSNumber*> dictionaryWithCapacity:2];
    [param setObject:[NSNumber numberWithInteger:1] forKey:@"Hello"];
    [param setObject:[NSNumber numberWithInteger:2] forKey:@"World"];
    AFHTTPSessionManager* Manager = [[AFHTTPSessionManager alloc] initWithBaseURL:BaseUrl
                                                             sessionConfiguration:nil];
    Manager.requestSerializer  = [AFJSONRequestSerializer  serializer];
    Manager.responseSerializer = [AFJSONResponseSerializer serializer];
    /*设置客户端的响应格式*/
    Manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [Manager POST:@"TestJsonData" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@",uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        /*返回值已经是字典类型了*/
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

你可能感兴趣的:(json,post,AFNetworking)