使用40个参数发布请求AFNetworking

时间:2017-03-14 10:22:55

标签: ios objective-c afnetworking

我必须向api发布一些数据。我需要向服务器发送40个参数。在这40个参数中我应该发送4个图像作为NSData。为此我想使用AFNetWorking.In AFNetworking我应该使用哪一个?

2 个答案:

答案 0 :(得分:0)

创建一个NSMutableDictionary并像这样添加所有40个参数,然后执行发布请求

- (void)submitLoginRequest:(NSString *)email password:(NSString *)password {

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];



    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:email forKey:@"Email"];
    [dict setValue:password forKey:@"Password"];



    [manager POST:@"http://www.google.com/api/" parameters:dict success:^(NSURLSessionTask *task, id responseObject) {

        if (responseObject == [NSNull null]) {

        }else {

            NSLog(@"response type : %@", NSStringFromClass([responseObject class]));
            NSLog(@"response type : %@", responseObject);

        }

    } failure:^(NSURLSessionTask *task, NSError *error) {

        NSLog(@"AFHTTPSession Failure : %@", [error localizedDescription]);
    }];

}

答案 1 :(得分:0)

我建议你写这些代码

NSDictionary *registerParameters=@{@"customer_firstname":credentials[@"firstname"],
                                           @"customer_lastname":credentials[@"lastname"],
                                           @"email":credentials[@"email"],
                                           @"passwd":credentials[@"password"],
                                           @"mobile_number":credentials[@"mobile"],
                                           @"device_type" : @"2",                                       
                                           @“language_id”:[NSNumber numberWithInteger:[RTGlobalValues sharedGlobalValues].selectedLanguageId]
                                           };
       NSDictionary *methodParamsDictionary =@{
                                                @“action”:@“register",
                                                @“controller”:kControllerLogin
                                                };
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    //AFHTTPSessionManager *session = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    session.requestSerializer = [AFJSONRequestSerializer serializer];
    [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 NSString *URL_String = [NSString stringWithFormat:@"http://www.requesturl/%@”,methodName];

        NSLog(@"URL string %@",URL_String);

        [session POST:URL_String parameters:inputParameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            NSLog(@"%@",imageName);
            [formData appendPartWithFileData:uploadImageData name:@"image" fileName:imageName mimeType:@"image/png"];

        } progress:^(NSProgress * _Nonnull uploadProgress) {

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSDictionary *dataObj = (NSDictionary *)responseObject;
            NSLog(@"%@", dataObj);
            completeBlock(dataObj);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            NSLog(@"%s :%@",__PRETTY_FUNCTION__,error.description);

            failBlock()
        }];

我希望这可以帮助你...