获取网络数据的两种方法 简单示例

#pragma mark--

#pragma mark 按键响应函数

//1.get方法上传参数、获取数据

- (IBAction)getMsgWithGet:(id)sender {

    NSString *baseUrl=@"http://getMsgWithGet.php";

    NSString *withString=@"name=xxx&msg=我要发送的信息";

    

    NSString *urlString=[NSString stringWithFormat:@"%@?%@",baseUrl,withString];

    NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request=[NSURLRequest requestWithURL:url];

    

    [NSURLConnection connectionWithRequest:request delegate:self];

}

//2.post方法上传参数、获得数据

- (IBAction)getMsgWithPost:(id)sender {

    NSString *baseUrl=@"http://getMsgWithPost.php";

    NSString *withString=@"name=zzz&msg=我要通过Post发送的信息";


    NSURL *url=[NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"post"];


    NSData *parmData=[withString dataUsingEncoding:NSUTF8StringEncoding];

    [request setHTTPBody:parmData];

    

    //可以通过这种方式在httpheadfiled中添加额外的信息

    [request addValue:[NSString stringWithFormat:@"%d",parmData.length] forHTTPHeaderField:@"Content-Length"];

    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    

    [NSURLConnection connectionWithRequest:request delegate:self];

}

#pragma mark--

#pragma mark NSURLConnection协议方法

//连接成功后调用

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons{

    netData.length=0;

    NSHTTPURLResponse *res=(NSHTTPURLResponse *)respons;

    NSLog(@"%@",res.allHeaderFields);

}

//接收数据

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

    [netData appendData:data];

}

//数据接收完成

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSString *resultString=[[NSString alloc]initWithData:netData encoding:NSUTF8StringEncoding];

    self.myLabel.text=resultString;

}



pragma mark--

#pragma mark 3.ASI协议方法示例

//导入头文件

#import "ASIHTTPRequest.h"

#import "ASIFormDataRequest.h"

-(void *)ASIgetDataWithID:(id)alumbID andWithModelIndex:(int)modelIndex

{  

    NSString *baseUrl = [NSString stringWithFormat:@"https://api.douban.com/v2/online/%@/photos",alumbID];

    NSURL *url = [NSURL URLWithString:baseUrl];


    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    __weak ASIHTTPRequest *wRequest = request;


//    [wRequest setCompletionBlock:^{//这样子士不行的

    [request setCompletionBlock:^{

        NSDictionary *phtotDict = [NSJSONSerialization JSONObjectWithData:wRequest.responseData options:NSJSONReadingMutableContainers error:nil];

        pArr = [NSMutableArray array];

        //封装数据为model

        NSMutableArray *mtuPhtotArr = phtotDict[@"photos"];

        for(int j = 0;j < mtuPhtotArr.count; j++)

        {

            NSDictionary *phtot = [NSDictionary dictionary];

            phtot = mtuPhtotArr[j];

            

            NSString *phtotUrl = phtot[@"image"];

            

            [pArr addObject:phtotUrl];

        }


        LYGDataModel *model = myMutArr[modelIndex];

        model.cellImageArr = pArr;

        [myTableView reloadData];

    }];

   

    [request setFailedBlock:^{

        NSLog(@"%@",request.error);

    }];

    [request startAsynchronous];


}



转载于:https://my.oschina.net/starmier/blog/203889

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值