afnetworking 3.0中具有图像和其他参数的多部分数据



我在使用其他参数上传图像时有问题。使用afnetworking 3.0

com.alamofire.serialization.Response.Data =< 7b227374 61747573 223A2246 41494C55 5245222C 22737461 74757343 6F646522 3A3A333A3A3A3A3A 3A3A3A3A3A3A3A3A3AA 3A3A3A3AA 3A3A3A3A3AA 3A3A3A3AA 3A 3A3A3A3AA 3A3A3A3AA 3A3A3A3A3AA 7D> }我得到

NSMutableDictionary *getUpdates= [[NSMutableDictionary alloc]init];
NSString*userID=@"3";
[getUpdates setObject:userID forKey:@"userId"];
[getUpdates setObject:phoneNumTF.text forKey:@"mobileNumber"];
[getUpdates setObject:nameTF.text forKey:@"name"];
[getUpdates setObject:workZipCodeTF.text forKey:@"work"];
[getUpdates setObject:homeZipCodeTF.text forKey:@"home"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:kUpdateProfile_Post parameters:getUpdates constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:ImageData
                                name:@"files"
                            fileName:@"picture" mimeType:@"image/jpeg"];
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];

在这里我需要像这样的数据发送图片:文件名 json:{" name":" harish"," work":92121," userId":3," mobilenumber":33663366," home":" 500025"}

有帮助吗?

//包含帖子参数的字典。您可以设置服务器接受或编程以接受的帖子参数。

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString*userID = [def objectForKey:@"UserId"];
NSMutableDictionary *getUpdates= [[NSMutableDictionary alloc]init];
[getUpdates setObject:userID forKey:@"userId"];
[getUpdates setObject:phoneNumTF.text forKey:@"mobileNumber"];
[getUpdates setObject:nameTF.text forKey:@"name"];
[getUpdates setObject:workZipCodeTF.text forKey:@"work"];
[getUpdates setObject:homeZipCodeTF.text forKey:@"home"];

[MBProgressHUD showHUDAddedTo:self.view animated:true];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // only needed if the server is not returning JSON; if web service returns JSON, remove this line

[manager POST:kUpdateProfile_Post parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:getUpdates options:0 error:&error];
    NSAssert(jsonData, @"Failure building JSON: %@", error);

    NSDictionary *jsonHeaders = @{@"Content-Disposition" : @"form-data; name="json"",
                                  @"Content-Type"        : @"application/json"};
    [formData appendPartWithHeaders:jsonHeaders body:jsonData];
    if (ImageData) {
        [formData appendPartWithFileData:ImageData name:@"picture"
                                fileName:@"image.jpg"
                                mimeType:@"image/jpeg"];
    }
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                         options:kNilOptions
                                                           error:&error];
    NSLog(@"error appending part: %@", json);
    [MBProgressHUD hideHUDForView:self.view animated:true];
    [self.navigationController popViewControllerAnimated:YES];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
    [MBProgressHUD hideHUDForView:self.view animated:true];
}];

最后,我得到了这个代码

两种可能性,

  • 可能是您要发送发送一些参数名称不同
  • 服务器开发人员在后端做错了什么

与他讨论这个问题,请您在提出请求时调试每行。

用您的代码检查以下内容:

1)确保 Imagedata 不应是 nil

2)使用您的服务器 Mine Type 检查矿山类型。可能两者都不同。

3)用您的API名称检查关键名称。

相关内容

  • 没有找到相关文章

最新更新