
NSURLRequest
默认情况会将NSURLConnection添加当前线程到RunLoop,如果是在子线程中调用NSURLConnection可能会有问题, 因为子线程默认没有RunLoop
如何让代理方法在子线程中执行?
注意:
NSURLConnection是异步请求
// 1.发送请求 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://XXXXX/resources/images/minion_01.png"]] ; NSRunLoop *runloop = [NSRunLoop currentRunloop]; NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; [conn setDelegateQueue:[[NSOperationQueue alloc] init]]; // 2.启动runLoop // 默认情况下子线程没有RunLoop, 所以需要启动 [runloop run]; // 3.如果手动发送请求, 系统内部会自动帮子线程创建一个RunLoop // NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; // [conn start];
同步请求
// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"http://XXXXXXX/login?username=XXX&pwd=XXX&type=JSON"];
// 2.根据URL创建NSURLRequest对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.利用NSURLConnection对象发送请求
/*
第一个参数: 需要请求的对象
第二个参数: 服务返回给我们的响应头信息
第三个参数: 错误信息
返回值: 服务器返回给我们的响应体
*/
NSHTTPURLResponse *response = nil; // 真实类型
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"response = %@", response.allHeaderFields);
// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"http://XXXXX/login?username=XXX&pwd=XXX&type=JSON"];
// 2.根据URL创建NSURLRequest对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.利用NSURLConnection对象发送请求
/*
第一个参数: 需要请求的对象
第二个参数: 回调block的队列, 决定了block在哪个线程中执行
第三个参数: 回调block
*/
// 注意点: 如果是调用NSURLConnection的同步方法, 会阻塞当前线程
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"http://XXXXX/login"];
// 2.根据URL创建NSURLRequest对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1设置请求方式
// 注意: POST一定要大写
request.HTTPMethod = @"POST";
// 2.2设置请求体
// 注意: 如果是给POST请求传递参数: 那么不需要写?号
request.HTTPBody = [@"username=XXX&pwd=XXX&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
// 3.利用NSURLConnection对象发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 1.创建URL
NSURL *url = [NSURL URLWithString:@"http://XXXXX/resources/images/minion_02.png"];
// 2.根据URL创建NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.利用NSURLConnection发送请求
/*
// 只要调用alloc/initWithRequest, 系统会自动发送请求
[[NSURLConnection alloc] initWithRequest:request delegate:self];
*/
/*
// startImmediately: 如果传递YES, 系统会自动发送请求; 如果传递NO, 系统不会自动发送请求
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn start];
*/
[NSURLConnection connectionWithRequest:request delegate:self];
代理方法
#PRagma mark - NSURLConnectionDataDelegate
/*
只要接收到服务器的响应就会调用
response:响应头
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%s", __func__);
}
/*
接收到服务器返回的数据时调用(该方法可能调用一次或多次)
data: 服务器返回的数据(当前这一次传递给我们的, 并不是总数)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%s", __func__);
}
/*
接收结束时调用
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __func__);
}
/*
请求错误时调用(请求超时)
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s", __func__);
}
// 1.创建URL
NSString *urlStr = @"http://XXXXXX/login2?username=博客园&pwd=XXX&type=JSON";
NSLog(@"转换前:%@", urlStr);
// 2.对URL进行转码
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];