NSMutableString 的 appendFormat 不附加更多一项
appendFormat of NSMutableString not append more one item
我用了NSMutableString
,用了appendFormat
函数,appendFormat设置了边界和第一张图片,没有再追加任何项目,我不知道错误在哪里。
结果调试:-
2016-03-08 15:52:56.385 PropertyTurkey[474:82387]
Body-----=-----011000010111000001101001
Content-Disposition:form-data; name="image[]";
filename="/var/mobile/Containers/Data/Application/A3EA5D3E-377E-4E16-9541-580BB57E211E/Documents/1.jpg"
ÿØÿà
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",
@"cache-control": @"no-cache" };
NSArray *parameters = @[ @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[0]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[1]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[2]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[3]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[4]]},
@{ @"name": @"rea_title", @"value": [NSString stringWithFormat:@"%@",_realEstateTitle] },
@{ @"name": @"why_buy_property", @"value": @"why_buy_property" },
@{ @"name": @"cit_id", @"value": [NSString stringWithFormat:@"%@",_cit_id] },
@{ @"name": @"are_id", @"value": [NSString stringWithFormat:@"%@",_are_id] },
@{ @"name": @"garden_exterior", @"value":[NSString stringWithFormat:@"%@",_desOfGardenAndExter] },
@{ @"name": @"home_interior", @"value":[NSString stringWithFormat:@"%@",_desOfHomeInterior] },
@{ @"name": @"property_desc", @"value":[NSString stringWithFormat:@"The property desc field is required."] },
@{ @"name": @"typ_id", @"value": [NSString stringWithFormat:@"%@",_typ_id] },
@{ @"name": @"rea_bedrooms", @"value": [NSString stringWithFormat:@"%@",_bedrooms] },
@{ @"name": @"rea_bathrooms", @"value": [NSString stringWithFormat:@"%@",_bathrooms]},
@{ @"name": @"rea_living_space", @"value": [NSString stringWithFormat:@"%@",_livingSpaceSqm] },
@{ @"name": @"sta_id", @"value": [NSString stringWithFormat:@"%@",_sta_id]},
@{ @"name": @"rea_price", @"value": [NSString stringWithFormat:@"%@",_price]} ];
NSString *boundary = @"---011000010111000001101001";
NSError *error;
NSMutableString *body = [[NSMutableString alloc]init];
for (NSDictionary *param in parameters) {
[body appendFormat:@"--%@\r\n", boundary];
if (param[@"fileName"]) {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
//[body appendFormat:@"Content-Type: %@\r\n\r\n", headers[@"content-type"]];
NSLog(@"%@",headers[@"content-type"]);
[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSISOLatin1StringEncoding error:&error]];
if (error) {
NSLog(@"%@", error);
}
} else {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
[body appendFormat:@"%@", param[@"value"]];
}
NSLog(@"Body-----=%@",body);
}
[body stringByAppendingFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.123/api/v1/sellProperty?api_key=%@&auth_token=%@&device_token=%@",apiKey,check,deviceToken]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
NSLog(@"%@",headers);
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"%@", httpResponse);
NSLog(@"res %@ serializedData %@", response,serializedData);
}
}];
[dataTask resume];
您正在使用创建新字符串的 stringByAppendingFormat,而不是向现有字符串添加任何内容。
当我替换这一行时
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"content-type" forHTTPHeaderField:@"multipart/form-data"];
[manager.requestSerializer setValue:@"cache-control" forHTTPHeaderField:@"no-cache"];
[manager POST:@"http://182.1698.133.45873/sellProperty?" parameters:par constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int x=0;
for (UIImage *imag in _chosenImages)
{
//[formData appendPartWithFormData:imageData name:@"image[]"];
NSLog(@"%@",imag);
NSData *imageData = UIImageJPEGRepresentation(imag, 0.5);
NSLog(@"%@",imageData);
[formData appendPartWithFileData:imageData name:@"image[]" fileName:[NSString stringWithFormat:@"%d.jpg",x] mimeType:@"image/jpeg"];
x++;
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
其工作正常。
我用了NSMutableString
,用了appendFormat
函数,appendFormat设置了边界和第一张图片,没有再追加任何项目,我不知道错误在哪里。
结果调试:-
2016-03-08 15:52:56.385 PropertyTurkey[474:82387] Body-----=-----011000010111000001101001
Content-Disposition:form-data; name="image[]"; filename="/var/mobile/Containers/Data/Application/A3EA5D3E-377E-4E16-9541-580BB57E211E/Documents/1.jpg"
ÿØÿà
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",
@"cache-control": @"no-cache" };
NSArray *parameters = @[ @{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[0]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[1]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[2]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[3]]},
@{ @"name": @"image[]", @"fileName":[NSString stringWithFormat:@"%@",_chosenImagesPath[4]]},
@{ @"name": @"rea_title", @"value": [NSString stringWithFormat:@"%@",_realEstateTitle] },
@{ @"name": @"why_buy_property", @"value": @"why_buy_property" },
@{ @"name": @"cit_id", @"value": [NSString stringWithFormat:@"%@",_cit_id] },
@{ @"name": @"are_id", @"value": [NSString stringWithFormat:@"%@",_are_id] },
@{ @"name": @"garden_exterior", @"value":[NSString stringWithFormat:@"%@",_desOfGardenAndExter] },
@{ @"name": @"home_interior", @"value":[NSString stringWithFormat:@"%@",_desOfHomeInterior] },
@{ @"name": @"property_desc", @"value":[NSString stringWithFormat:@"The property desc field is required."] },
@{ @"name": @"typ_id", @"value": [NSString stringWithFormat:@"%@",_typ_id] },
@{ @"name": @"rea_bedrooms", @"value": [NSString stringWithFormat:@"%@",_bedrooms] },
@{ @"name": @"rea_bathrooms", @"value": [NSString stringWithFormat:@"%@",_bathrooms]},
@{ @"name": @"rea_living_space", @"value": [NSString stringWithFormat:@"%@",_livingSpaceSqm] },
@{ @"name": @"sta_id", @"value": [NSString stringWithFormat:@"%@",_sta_id]},
@{ @"name": @"rea_price", @"value": [NSString stringWithFormat:@"%@",_price]} ];
NSString *boundary = @"---011000010111000001101001";
NSError *error;
NSMutableString *body = [[NSMutableString alloc]init];
for (NSDictionary *param in parameters) {
[body appendFormat:@"--%@\r\n", boundary];
if (param[@"fileName"]) {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
//[body appendFormat:@"Content-Type: %@\r\n\r\n", headers[@"content-type"]];
NSLog(@"%@",headers[@"content-type"]);
[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSISOLatin1StringEncoding error:&error]];
if (error) {
NSLog(@"%@", error);
}
} else {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
[body appendFormat:@"%@", param[@"value"]];
}
NSLog(@"Body-----=%@",body);
}
[body stringByAppendingFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.123/api/v1/sellProperty?api_key=%@&auth_token=%@&device_token=%@",apiKey,check,deviceToken]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
NSLog(@"%@",headers);
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"%@", httpResponse);
NSLog(@"res %@ serializedData %@", response,serializedData);
}
}];
[dataTask resume];
您正在使用创建新字符串的 stringByAppendingFormat,而不是向现有字符串添加任何内容。
当我替换这一行时
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"content-type" forHTTPHeaderField:@"multipart/form-data"];
[manager.requestSerializer setValue:@"cache-control" forHTTPHeaderField:@"no-cache"];
[manager POST:@"http://182.1698.133.45873/sellProperty?" parameters:par constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
int x=0;
for (UIImage *imag in _chosenImages)
{
//[formData appendPartWithFormData:imageData name:@"image[]"];
NSLog(@"%@",imag);
NSData *imageData = UIImageJPEGRepresentation(imag, 0.5);
NSLog(@"%@",imageData);
[formData appendPartWithFileData:imageData name:@"image[]" fileName:[NSString stringWithFormat:@"%d.jpg",x] mimeType:@"image/jpeg"];
x++;
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
其工作正常。