码迷,mamicode.com
首页 > Web开发 > 详细

网络编程练习 -- POST-JSON数据

时间:2014-07-07 09:22:26      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   数据   os   

LWTViewController.m

bubuko.com,布布扣
//
//  LWTViewController.m
//  网络编程练习 -- POST-JSON数据
//
//  Created by apple on 14-7-2.
//  Copyright (c) 2014年 lwt. All rights reserved.
//

#import "LWTViewController.h"

@interface LWTViewController ()

@end

@implementation LWTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.24:8080/MJServer/resources/order"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSDictionary *json = @{
                           @"order_id" : @"123",
                           @"user_id" : @"456",
                           @"shop" : @"Toll"
                           };
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];

    request.HTTPBody = data;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%d", data.length);
    }];
}

@end
View Code

 

网络编程练习 -- POST-JSON数据,布布扣,bubuko.com

网络编程练习 -- POST-JSON数据

标签:style   blog   http   color   数据   os   

原文地址:http://www.cnblogs.com/wentianblog/p/3820775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!