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

IOS网络篇

时间:2015-03-02 11:02:05      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

一、同步GET请求方法

-(void)synchronizationGet

{

    NSString *strURL = @"http://olasapi.sinaapp.com//index.php";

    NSURL *url = [NSURL URLWithString:strURL];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSLog(@"请求完成");

    

    

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"%@",dic);

}

二、异步get

-(void)asynchronousGet

{

  

    NSString *strURL = @"http://olasapi.sinaapp.com//index.php";

    NSURL *url = [NSURL URLWithString:strURL];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    

    if (connection) {

        _data = [NSMutableData new];

    }

}

#pragma mark - NSURLConnection回调方法

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [_data appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"请求完成");

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingAllowFragments error:nil];

    NSLog(@"%@",dic);

}

三、异步POST

-(void)asynchronousPost

{

    NSString *strURL = @"http://olasapi.sinaapp.com//index.php";

    NSURL *url = [NSURL URLWithString:strURL];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setHTTPMethod:@"POST"];

//    [request setHTTPBody:@""]

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    

    if (connection) {

        _data = [NSMutableData new];

    }

 

}

代理方法同get异步中的代理方法

IOS网络篇

标签:

原文地址:http://www.cnblogs.com/zhanggui/p/4308079.html

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