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

post method how to send only required parameters objective-c

我正在将多部分数据连同图像一起发送到服务器文本,并且 voice/image 在这种情况下,语音是可选的,当我不发送图像数据或语音时,应用程序崩溃了,请帮助解决这个问题!

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

    }
}

根据您的要求设置布尔值

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

        }
}
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:^ ...

如果您不发送图像或音频,则您的图像数据和音频数据包含 nil(您无法发送包含 nil 数据的图像或音频)。