post方法如何只发送所需的参数objective-c

时间:2016-08-02 05:28:08

标签: ios objective-c multipartform-data afhttprequestoperation

我发送多部分数据到服务器文本以及图像和语音/图像和语音都是可选的,在这种情况下,当我没有发送图像数据或语音应用程序崩溃时,请帮忙!

-(void)uploadphoto{
 NSString* mid= @"1";
    NSString*userid=@"13"; //[[NSUserDefaults standardUserDefaults] valueForKey:kUserID];

    imageData = UIImagePNGRepresentation (thumbnail.image);

     AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];

    NSDictionary *parameters = @{@"UserID":userid, @"Name": name_TF.text,@"MandalID":mid,@"Address":address_TV.text,@"PinCode":pincode_TF.text,@"Email":emailid_TF.text,@"Dese":grivence_TV.text};

    AFHTTPRequestOperation *op = [manager POST:@"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];

    [formData appendPartWithFileData:audioData name:@"file" fileName:@"Audio.m4a" mimeType:@"audio/.mp4 .m4a"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];

    }
}

2 个答案:

答案 0 :(得分:1)

AFHTTPRequestOperation *op = [manager POST:@"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  

  if (imageData!=nil) 
    [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];


    if (audioData!=nil) 
    [formData appendPartWithFileData:audioData name:@"file" fileName:@"Audio.m4a" mimeType:@"audio/.mp4 .m4a"];

} success:^ ...

如果你没有发送图像或音频,你的图像数据和音频audiodata包含nil(你不能发送没有数据的图像或音频)。

答案 1 :(得分:0)

根据您的要求设置bool

 -(void)uploadphoto{

    BOOL isImgData = YES; //set according to avaibility
    BOOL isVoiceData = YES;//set according to avaibility

     NSString* mid= @"1";
        NSString*userid=@"13"; //[[NSUserDefaults standardUserDefaults] valueForKey:kUserID];

        imageData = UIImagePNGRepresentation (thumbnail.image);

         AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];

        NSDictionary *parameters = @{@"UserID":userid, @"Name": name_TF.text,@"MandalID":mid,@"Address":address_TV.text,@"PinCode":pincode_TF.text,@"Email":emailid_TF.text,@"Dese":grivence_TV.text};

        AFHTTPRequestOperation *op = [manager POST:@"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    if(isImgData){

        [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
    }

    if(isVoiceData){

        [formData appendPartWithFileData:audioData name:@"file" fileName:@"Audio.m4a" mimeType:@"audio/.mp4 .m4a"];

    }
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@ ***** %@", operation.responseString, error);
        }];
        [op start];

        }
}