简体   繁体   中英

Post Request AFNetworking with 40 Parameters

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

Create a NSMutableDictionary and add all the 40 Parameters like this and then do a post request

- (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]);
    }];

}

I suggest you to write the code like these

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()
        }];

i hope this help you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM