zoukankan      html  css  js  c++  java
  • [iOS 多线程 & 网络

    A.上传JSON
    1.思路:
    必须使用POST方法才能上传大量JSON数据
    设置请求头:设置Content-Type
    设置请求体,JSON实际相当于字典,可以用NSDictionary
    NSJSONSerialization把字典数据转换成JSON二进制
     
     
    2.实现
     1 //
     2 //  ViewController.m
     3 //  PostJsonDemo
     4 //
     5 //  Created by hellovoidworld on 15/1/28.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 
    11 @interface ViewController ()
    12 - (IBAction)postJson;
    13 
    14 @end
    15 
    16 @implementation ViewController
    17 
    18 - (void)viewDidLoad {
    19     [super viewDidLoad];
    20     // Do any additional setup after loading the view, typically from a nib.
    21 }
    22 
    23 - (IBAction)postJson {
    24     // 1.创建请求
    25     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/acceptJson"];
    26     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    27     // 设置post发送
    28     request.HTTPMethod = @"POST";
    29    
    30     // 2.设置请求头
    31     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    32    
    33     // 3.设置请求体
    34     NSDictionary *json = @{@"name":@"tom",
    35                            @"age":@"21"};
    36     request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
    37    
    38    
    39     // 4.发送请求
    40     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    41         NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]);
    42     }];
    43    
    44 }
    45 @end
     
     
    B.多值参数
    1.概念
    一个参数名对应多个参数值
    http://localhost:8080/MyTestServer/upload?type=aaa&type=bbb&type=ccc
    这样在服务器接收到的就是一个数组
     
     
  • 相关阅读:
    类例程_java战斗程序
    "类"的讲稿
    象棋中“车”的攻击范围_C#
    面向对象_方法_例题
    入门例子
    MyBatis
    MyBatis
    MyBatis
    Spring
    Hibernate学习
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4257684.html
Copyright © 2011-2022 走看看