AFNetworking 3.0中包含图像和其他参数的多部分数据

时间:2017-02-16 10:59:51

标签: ios objective-c iphone afnetworking afnetworking-3

使用其他参数上传图片时出现问题。使用AFNetworking 3.0

com.alamofire.serialization.response.error.data =< 7b227374 61747573 223a2246 41494c55 5245222c 22737461 74757343 6f646522 3a353030 7d>,NSLocalizedDescription =请求失败:内部服务器错误(500)}我正在

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:{&#34; name&#34;:&#34; harish&#34;,&#34; work&#34;:92121,&#34; userId&#34;:3,&#34; mobileNumber&#34 ;:33663366,&#34; home&#34;:&#34; 500025&#34; }

任何帮助?

3 个答案:

答案 0 :(得分:1)

//保存帖子参数的词典。您可以设置服务器接受或编程接受的帖子参数。

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 :(得分:0)

两种可能性,

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

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

答案 2 :(得分:0)

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

1)确保 ImageData 不应 nil

2)使用服务器我的类型检查我的类型。可能两者都不同。

3)用您的api名称检查密钥名称。