zoukankan      html  css  js  c++  java
  • 发送json给服务器

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // 1.URL
        NSURL *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];
            }
        }];
        
    }

    
    

      

  • 相关阅读:
    MySql——编程
    MySql——事务控制语言(DTL)
    wamp-php 集成环境的基础配置
    用户的 添加 权限 MySql远程登录
    使用模拟器调试移动端
    new关键字对构造函数做了什么
    H5与Native交互之JSBridge技术
    拍照上传头像图像旋转的问题
    Vue实现用户自定义上传头像裁剪
    H5图片压缩上传
  • 原文地址:https://www.cnblogs.com/seeworld/p/6006266.html
Copyright © 2011-2022 走看看