此处有大坑一个,后台需要json数据,本以为json和字典一样发送,结果后台接收不到,想尽了一切办法。。。。
直接上代码
- (void)didConfirmButtonClickWithItem:(OrderContentModel *)item {
{
// 1.创建请求
NSURL *url = [NSURLURLWithString:@"http://192.168.31.125/url"];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
request.HTTPMethod =@"POST";
// 2.设置请求头
[request setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
// 3.设置请求体
NSMutableDictionary *parameters = [NSMutableDictionarydictionary];
NSMutableDictionary * dishDict = [NSMutableDictionary dictionary];
for (int i = 0; i<self.paymentOrder.ordersArray.count; i++) {
NSMutableDictionary * dict = [NSMutableDictionarydictionary];
DishBriefItem * brief =self.paymentOrder.ordersArray[i];
[self.paymentOrder.ordersArray[i]dish_id];
dict[@"dish_id"] = brief.dish_id;
NSMutableArray * peitaoArray = [NSMutableArrayarray];
NSMutableArray * peitaoNumArray = [NSMutableArrayarray];
for (int j =0; j
DishPeiTaoGroupItem *group = brief.peitao[j];
for (int k =0; k < group.arr.count; k++) {
DishSubPeiTaoItem *sub = group.arr[k];
self.subItem = sub;
NSLog(@"subName:%@", sub.name);
if (sub.peitaoCount !=0) {
NSLog(@"%ld",(long)sub.peitaoCount);
[peitaoArray addObject:sub.garnish_id];
[peitaoNumArray addObject:[NSStringstringWithFormat:@"%ld",(long)sub.peitaoCount]];
}
}
}
NSString * peitaoString = [peitaoArraycomponentsJoinedByString:@","];
dict[@"peitao"] = peitaoString;
NSLog(@"peitaoTest:%lu", (unsignedlong)brief.teshuyaoqiu.count);
NSString *peitaoNumString = [peitaoNumArraycomponentsJoinedByString:@","];
dict[@"peitaoNum"] = peitaoNumString;
NSMutableArray * yaoqiuArray = [NSMutableArrayarray];
if ( brief.teshuyaoqiu.count >0) {
NSMutableArray * teshuyaoqiuArray = [NSMutableArrayarray];
for (int index =0; index < brief.teshuyaoqiu.count; index ++ ) {
DishSpecialOrderItem *item = brief.teshuyaoqiu[index];
NSLog(@"selectStatus:%d",item.selectedStatus);
if (item.selectedStatus ==1) {
[teshuyaoqiuArray addObject:item];
}
}
for (int index =0; index < teshuyaoqiuArray.count; index ++ ) {
DishSpecialOrderItem *item = teshuyaoqiuArray[index];
[yaoqiuArray addObject:item.label_id];
}
}
dict[@"teshuyaoqiu"] = [yaoqiuArraycomponentsJoinedByString:@","];
NSString * key = [NSStringstringWithFormat:@"%d",i];
dishDict[key] = dict;
DishBriefItem *numItem =self.myOrdersArray[i];
NSString *string = dishDict[[NSStringstringWithFormat:@"%ld",(long)i]][@"dish_id"];
int dishNumm = [stringintValue];
NSNumber *dishNum = [NSNumbernumberWithInt:dishNumm];
NSString *stringing = [NSStringstringWithFormat:@"%ld",(long)numItem.orderCount];
int garnish = [stringingintValue];
NSNumber *garnishNum = [NSNumbernumberWithInt:garnish];
NSDictionary *dic = [NSDictionarydictionary];
dic = @{
@"dish_id":dishNum,
@"part":garnishNum,
@"garnish_id":dict[@"peitao"],
@"garnish_part":dict[@"peitaoNum"],
@"label_id":dict[@"teshuyaoqiu"],
};
[self.arrayaddObject:dic];
}
NSString *stringinging =self.tableid;
int tableid = [stringingingintValue];
NSNumber *table = [NSNumbernumberWithInt:tableid];
parameters[@"code"] = [NSStringstringWithFormat:@"%u",arc4random()];
parameters[@"table_id"] = table;
parameters[@"claim"] =self.beizhuContentTextField.text;
parameters[@"dishes"] =self.array;
parameters [@"allprice"] =self.priceLabel.text;
NSData *data = [NSJSONSerializationdataWithJSONObject:parametersoptions:NSJSONWritingPrettyPrintederror:nil];
request.HTTPBody = data;
// 4.发送请求
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {
NSLog(@"------------%@-----------",response);
[selfremoveLeftContentViewFromSuperView];
[self.rightTabelViewreloadData];
}];
}
}