- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{// 1.URLNSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"];// 2.请求NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];// 3.请求方法request.HTTPMethod = @"POST";// 4.设置请求体(请求参数)// 创建一个描述订单信息的JSON数据NSDictionary *orderInfo = @{@"shop_id" : @"1243324",@"shop_name" : @"啊哈哈哈",@"user_id" : @"899"};//把字典转换为可以传输的NSData类型NSData *json = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil];request.HTTPBody = json;// 5.设置请求头:这次请求体的数据不再是普通的参数,而是一个JSON数据[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];// 6.发送请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {if (data == nil || connectionError) return;NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];NSString *error = dict[@"error"];if (error) {[MBProgressHUD showError:error];} else {NSString *success = dict[@"success"];[MBProgressHUD showSuccess:success];}}];}
iOS开发 -- 发送JSON数据给服务器
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:iOS开发--网络下载
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
















