热门IT资讯网

同步GET

发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,创建get请求,1.将网址初始化成一个OC字符串对象.NSString * urlString = [NSString stringWithFormat:@"%@?query=%@®ion=%@



创建get请求,

1.将网址初始化成一个OC字符串对象.
NSString * urlString = [NSString stringWithFormat:@"%@?query=%@®ion=%@&output=json&ak=6E823f587c95f0148c19993539b99295",kBusinessInfoURL,@"银行",@"济南"];
如果网址中存在中文,进行转码

NSString * urlString = @"http://p_w_picpath.zcool.com.cn/56/13/1308200901454.jpg";

NSString * newURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

2.构建网络URL对象
NSURL * url = [NSURL URLWithString:newURLString];
//3.创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];

3.创建同步连接.
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

4.解析

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);

0