我使用了NSMutableString
并使用了appendFormat
函数,appendFormat设置了边界和第一个图像,并且没有追加任何其他项目,我不知道错误在哪里。
导致调试:-
2016-03-08 15:52:56.385 PropertyTurkey [474:82387]
正文----- = ----- 011000010111000001101001
Content-Disposition:表单数据; 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来创建一个新字符串,而不向现有字符串添加任何内容。
关于ios - NSMutableString的appendFormat不能追加多个项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869339/