AFNetworking 3.0 中带有图像和其他参数的多部分数据
Multi-part data with image and other parameters in AFNetworking 3.0
使用其他参数上传图片时出现问题。使用 AFNetworking 3.0
com.alamofire.serialization.response.error.data=<7b227374 61747573 223a2246 41494c55 5245222c 22737461 74757343 6f646522 3a353030 7d>, NSLocalizedDescription=请求失败:内部服务器错误 (500)} 我得到[=11]
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);
}];
这里我需要发送这样的数据
picture:filename
json:{ "name": "harish", "work": 92121, "userId": 3, "mobileNumber": 33663366, "home": " 500025" }
有什么帮助吗?
两种可能,
- 可能是您发送的参数名称不同
- 服务器开发人员在后端做错了事
和他讨论这个问题,让他在你提出请求时调试每一行。
使用您的代码检查以下内容:
1) 确保 ImageData 不应该是 nil.
2) 检查矿机类型与你的服务器矿机类型。可能两者不同。
3) 使用您的 api 名称检查密钥名称。
// 包含 post 个参数的字典。您可以设置服务器接受或编程接受的 post 参数。
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];
}];
我终于得到了这段代码
使用其他参数上传图片时出现问题。使用 AFNetworking 3.0
com.alamofire.serialization.response.error.data=<7b227374 61747573 223a2246 41494c55 5245222c 22737461 74757343 6f646522 3a353030 7d>, NSLocalizedDescription=请求失败:内部服务器错误 (500)} 我得到[=11]
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);
}];
这里我需要发送这样的数据 picture:filename json:{ "name": "harish", "work": 92121, "userId": 3, "mobileNumber": 33663366, "home": " 500025" }
有什么帮助吗?
两种可能,
- 可能是您发送的参数名称不同
- 服务器开发人员在后端做错了事
和他讨论这个问题,让他在你提出请求时调试每一行。
使用您的代码检查以下内容:
1) 确保 ImageData 不应该是 nil.
2) 检查矿机类型与你的服务器矿机类型。可能两者不同。
3) 使用您的 api 名称检查密钥名称。
// 包含 post 个参数的字典。您可以设置服务器接受或编程接受的 post 参数。
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];
}];
我终于得到了这段代码