一:get请求网络数据
采用三方库请求get数据
系统的方式
1.遵守代理NSURLConnectionDelegate,NSURLConnectionDataDelegate
2.给一个地址,取问号前面的
NSURL *url = [NSURL URLWithString:@""];
3.创建一个对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
4.请求的地址其余的部分,问号之后的部分
5.
_conn = [NSURLConnection connectionWithRequest:request delegate:self];
12.开始解析
[_conn start];
13.方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_receiveData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//数据的处理
id result = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingMutableContainers error:nil];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"下载失败");
}
清空数据!!!!!!切忌!!!!!
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_receiveData.length = 0;
}
采用三方库请求get数据
1.采用三方库请求get数据,使用ASIHTTPRequest三方库
1.导入三方库ASIHTTPRequest
2.同样在Build Phases里面修改arc:-fno-objc-arc
3.导入系统的框架这里只是用来请求数据的
3-1.SystemConfiguration.framework
3-2.MobileCoreServices.framework
3-3.libz.1.1.3.dylib
3-4.CFNetwork.framework
4.开始处理请求数据
4-1.导入头文件#import "ASIHTTPRequest.h"
4-2.设置代理:ASIHTTPRequestDelegate只遵守这一个就可以,如果多遵守不实现,会出现意外的错误,切忌
4-3.创建对象
NSURL *url = [NSURL URLWithString:@""];
4-4.创建asihttprequest对象
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
4-5.设置代理
request.delegate = self
4-6.下载开始
[request startAsynchronous];
5.遵守两个代理方法。完成数据请求
5-1.下载失败
-(void)requestFailed:(ASIHTTPRequest *)request;
5-2.下载成功二进制数据存放的位置request.responseData
-(void)requestFinished:(ASIHTTPRequest *)request{
id result = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
}
二:post请求数据
采用三方库请求
2.采用三方库请求post数据,使用ASIHTTPRequest三方库
采用三方库的形式,来处理post请求
1.导入三方库ASIHTTPRequest
2.修改非arc的显示-fno=objc-arc
3.导入系统框架SystemConfiguration.framework
CFNetwork.framework
MobileCoreServices.framework
libz.1.1.3.dylib
4.开始解析
1.导入头文件#import "ASIFormDataRequest.h";
2.遵守代理ASIHTTPRequestDelegate
3. 给地址,post请求url给的是问号之前的字符串
NSURL *url = [NSURL URLWithString:@""];
4.request请求对象
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
5.请求方式
request.requestMethod = @"post";
6.设置代理
request.delegate = self;
7.将余下的内容使用setvalue方式解析
[request setPostValue:@"10"forKey:@"pageSize"];
8.异步请求
[request startAsynchronous];
9.实现两个代理方法
-(void)requestFinished:(ASIHTTPRequest *)request{
id result = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
}
-(void)requestFailed:(ASIHTTPRequest *)request;
系统的get请求方式
采用系统的post请求
1.遵守代理NSURLConnectionDelegate,NSURLConnectionDataDelegate
2.给一个地址,取问号前面的
NSURL *url = [NSURL URLWithString:@""];
3.创建一个可变的对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
4.请求的地址其余的部分,问号之后的部分
NSString *str = @"";
5.参数传递到请求体
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
6.请求参数
request.HTTPBody = data;
7.请求方式为post
request.HTTPMethod = @"post";
8.设置参数,请求头的参数,第一个是参数的大小,key值是content-length
NSString *len = [NSString stringWithFormat:@"%ld",data.length];
9.
[request setValue:len forHTTPHeaderField:@"content-length"];
10.设置类型,都是默认的
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
11.创建NSURLConnection
_conn = [NSURLConnection connectionWithRequest:request delegate:self];
12.开始解析
[_conn start];
13.方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_receiveData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//数据的处理
id result = [NSJSONSerialization JSONObjectWithData:_receiveData options:NSJSONReadingMutableContainers error:nil];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"下载失败");
}
清空数据!!!!!!切忌!!!!!
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
_receiveData.length = 0;
}