我是IOS的初学者,第一次写网络请求,这个是Get方法.解析的是Json数据,写了快递查询,公交查询,天气查询。
1、Request.h
#import <Foundation/Foundation.h>
@protocol FinishRequestDelegate <NSObject>
- (void)requestFinished:(NSMutableData *)severData;
@end
@interface Request :NSObject<NSURLConnectionDataDelegate>
@property(strong, nonatomic)id<FinishRequestDelegate> delegate;
@property(retain, nonatomic)NSMutableData *severData;
- (void)requestWithGetWithUrl:(NSString *)urlString;
@end
#import "Request.h"
@implementation Request
NSInteger flag=0;
/*
1、NSURL:请求地址
2、NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法,请求头,请求体
3、NSURLConnection:负责发送请求,建立客户端与服务器的连接,发送NSURLRequest的数据给服务器,并收集来自服务器的相应数据
4、使用NSURLRequest发送请求的步骤:
(1)、创建一个NSURL对象,设置请求路径(设置请求路径)
(2)、传入NSURL创建的一个NSURLRequest对象,设置请求头和请求体(创建请求对象)
(3)、使用NSURLConnection发送NSURLRequest(发送请求)
5、发送请求的三个步骤:
(1)、设置请求路径
(2)、创建请求对象
(3)、发送请求
*/
- (void)requestWithGetWithUrl:(NSString *)urlString
{
NSLog(@"$$$$$$$");
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//有中文需转换,使用UTF-8
//1.设置URL,确定要访问的地址
NSURL *url = [NSURLURLWithString:urlString];
NSLog(@"urlString=%@", urlString);
//2、定义NSURLRequest,确定网络访问请求,在GET方法中直接使用即可
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:10.0f];//参数1地址,参数2缓存策略,参数3超时时间//NSURLResponse *response=nil;//NSError * error=nil;
//3、连接服务器
NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:request delegate:self];
//开始连接
[connectionstart];
//4.进行解析(返回数据)
self.severData = [NSMutableDatadata];//不是第四步,ARC=2,因为点语法操作会自动加1
//改错:
}
//请求的代理方法
//接收到服务器的响应,客户端做好准备,接收数据
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"服务器收到响应");
}
//接收服务器数据,多次执行
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"第%d次执行", ++flag);
[self.severDataappendData:data];
}
//接收数据完成,做后续处理
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// NSLog(@"___%@",self.severData);
[self.delegaterequestFinished:_severData];
}
//服务器请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
_severData = (NSMutableData *)[error.localizedDescriptiondataUsingEncoding:NSUTF8StringEncoding];
[self.delegaterequestFinished:_severData];
}
- (void)requestFinished:(NSMutableData *)severData
{
//Xcode自带解析类NSJSONSerialization从data中解析出数据放到字典中
NSMutableDictionary *dict = [NSJSONSerializationJSONObjectWithData:severData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",[dictobjectForKey:@"resultcode"]);
NSLog(@"%@",[dictobjectForKey:@"reason"]);
NSMutableDictionary * d = [dictobjectForKey:@"result"];//因为result的值是个字典,所以用字典接收
NSString *str2 = [d objectForKey:@"company"];//打印公司名称
NSLog(@"%@",str2);
self.pressTypeLabel1.text=str2;
NSString *str1 = [d objectForKey:@"com"];//
NSLog(@"%@",str1);
NSString *str34 = [d objectForKey:@"no"];//打印运单编号
NSLog(@"%@",str34);
self.numberLabel1.text=str34;
self.array=[dobjectForKey:@"list"];//list的对象是个数组,所以用数组接收
//快速遍历
for (NSMutableDictionary * cin self.array)//每个数组的对象又是个字典,所以用字典接收
{
NSString *str3 = [c objectForKey:@"datetime"];//打印相应的值
NSLog(@"%@",str3);
NSString *str4 = [c objectForKey:@"remark"];
NSLog(@"%@",str4);
NSString *str5 = [c objectForKey:@"zone"];
NSLog(@"%@",str5);
}
}
Json数据
{
"resultcode": "200",
"reason": "成功的返回",
"result": {
"company": "顺丰",
"com": "sf",
"no": "575677355677",
"list": [
{
"datetime": "2013-06-25 10:44:05",
"remark": "已收件",
"zone": "台州市"
},
{
"datetime": "2013-06-25 11:05:21",
"remark": "快件在 台州 ,准备送往下一站 台州集散中心 ",
"zone": "台州市"
},
{
"datetime": "2013-06-25 20:36:02",
"remark": "快件在 台州集散中心 ,准备送往下一站 台州集散中心 ",
"zone": "台州市"
},
{
"datetime": "2013-06-25 21:17:36",
"remark": "快件在 台州集散中心 ,准备送往下一站 杭州集散中心 ",
"zone": "台州市"
},
{
"datetime": "2013-06-26 12:20:00",
"remark": "快件在 杭州集散中心 ,准备送往下一站 西安集散中心 ",
"zone": "杭州市"
},
{
"datetime": "2013-06-27 05:48:42",
"remark": "快件在 西安集散中心 ,准备送往下一站 西安 ",
"zone": "西安市/咸阳市"
},
{
"datetime": "2013-06-27 08:03:03",
"remark": "正在派件..",
"zone": "西安市/咸阳市"
},
{
"datetime": "2013-06-27 08:51:33",
"remark": "派件已签收",
"zone": "西安市/咸阳市"
},
{
"datetime": "2013-06-27 08:51",
"remark": "签收人是:已签收 ",
"zone": "西安市/咸阳市"
}
],
"status": 1
},
"error_code": 0
}