iOS网络之09发送特殊数据到服务器(json,多值参数)

发送json到服务器
1,如果发送JSON数据,比如商城下单的时候发送下单信息给服务器,则必须在请求头中说明数据类型
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/order"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSDictionary *orderInfo = @{
                                @"shop_id":@"12341",
                                @"shop_name":@"zhang",
                                @"user_id":@"danfeng"
                                };
    
    NSData *data01 = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil];
    request.HTTPBody = data01;
    
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
        if(data == nil || connectionError) return ;
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        NSString *success = dict[@"success"];
        
        if(error){
            NSLog(@"%@",error);
        }else{
            NSLog(@"%@",success );
        }
    }];
}
发送多值参数到服务器
2,多值参数,也就是一个参数中有多个值,直接拼接成字符串放进去即可:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.103:8080/MJServer/weather"];
    NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    NSMutableString *body = [NSMutableString string];
    [body appendString:@"place=beijing"];
    [body appendString:@"&place=shanghai"];
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if(connectionError || data == nil){
            NSLog(@"request failed");
        }
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        NSString *weather = dict[@"weathers"];
        if(error){
            NSLog(@"%@",error);
        }else if(weather){
            NSLog(@"%@",weather);
        }
    }];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,993评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 12,867评论 6 13
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,698评论 0 15
  • JSON JSON和XML都是需要解析的 JSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据...
    JonesCxy阅读 2,058评论 2 10
  • 春风响,东风重沐暖阳曲 桃花开,绿叶双并粉红意 暖阳烘烘,春风昭昭 与诸君饮桃花酒与东山之上 看春光暖暖替北风寒昭...
    梵夜阅读 560评论 0 0

友情链接更多精彩内容