IOS_GET编码设置

//
//  PJViewController.m
//  GET
//
//  Created by pj on 14-8-3.
//  Copyright (c) 2014年 pj. All rights reserved.
//

#import "PJViewController.h"

@interface PJViewController ()

@end

@implementation PJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self get];
    NSString *str = @"{\"userId\":1,\"userName\":\"zhangsan\"}";
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL];
    NSLog(@"%@",dict);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)post
{
    NSString *urlString = [NSString stringWithFormat:@"http://www.baidu.com"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"a=2&b=2" dataUsingEncoding:NSUTF8StringEncoding];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"success%@%@",result,[NSThread currentThread]);
    }];
    NSLog(@"end");
}
- (void)get
{
    // get需要对编码进行转换
    NSString *urlString = [@"http://www.baidu.com/?a=2&b=2" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 在主线程上执行这条异步指令
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"success%@%@",result,[NSThread currentThread]);
    }];
    NSLog(@"end");
}

@end

 

posted @ 2014-08-03 17:56  宝贝,我永远都在  阅读(129)  评论(0)    收藏  举报