发送json给服务器
生活随笔
收集整理的這篇文章主要介紹了
发送json给服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- (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 *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];}}];}
轉載于:https://www.cnblogs.com/seeworld/p/6006266.html
總結
以上是生活随笔為你收集整理的发送json给服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GLSL三种修饰符区别与用途(unifo
- 下一篇: clr enabled Server C