• ios开发 将json格式数据上传服务器


    看了一些大小牛的资料其实就3步

    1.使用post 请求 ,因为get是不能上传的

    2.设置请求类型 , 讲你的json数据上传

    3.向服务器发送数据按照下面示例代码,就差不多了

     1     // 1.创建请求
     2     NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"];
     3     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
     4     request.HTTPMethod = @"POST";
     5     
     6     // 2.设置请求类型
     7     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
     8     
     9     // 3.上传json数据
    10     NSDictionary *json = @{
    11                            @"order_id" : @"1",
    12                            @"user_id" : @"1",
    13                            @"shop" : @"1"
    14                            };
    15     
    16 //    NSData --> NSDictionary
    17     // NSDictionary --> NSData
    18     NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    19     request.HTTPBody = data;
    20     
    21     // 4.发送请求
    22     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    23         NSLog(@"%d", data.length);
    24     }];
  • 相关阅读:
    杭电OJ 输入输出练习汇总
    七月读书笔记
    情报分析报告阅读笔记
    情报研究与分析入门阅读笔记
    旁观者攻击
    域前置技术相关学习
    CC攻击和C2的区别
    DNS投毒学习分析总结
    数字证书2.0版本学习总结
    《在树洞里》-感悟
  • 原文地址:https://www.cnblogs.com/liumingxin123/p/5473567.html
Copyright © 2020-2023  润新知