網(wǎng)絡(luò)上搜索關(guān)于ios網(wǎng)絡(luò)編程基本就首頁(yè)全是講的同一篇文章,被轉(zhuǎn)爛了。
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)西工免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
找了半天沒(méi)找到源文出處。下面是可以參考的一部分。
主要將了兩部分:1.網(wǎng)絡(luò)檢測(cè);2.簡(jiǎn)單的NSURLConnection鏈接以及設(shè)置代理。
問(wèn)了下朋友,基本說(shuō)現(xiàn)在都用
HTTP包裝開源項(xiàng)目ASIHTTPRequest。
但這邊我們還是從最原始的框架提供的API入手,后邊我再去看下這個(gè)。
這邊我就以最簡(jiǎn)單的例子來(lái)引入幾個(gè)常用的API中的類。
[cpp] view plaincopy
//
// NLViewController.m
// NetWorkTest
//
// Created by Nono on 12-5-16.
// Copyright (c) 2012年 NonoWithLilith. All rights reserved.
//
#import "NLViewController.h"
@interface NLViewController ()
@end
@implementation NLViewController
@synthesize label = _label;
@synthesize data = _data;
@synthesize connection = _connection;
- (void)dealloc{
[self.label release];
[self.data release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 400)];
self.label = label;
label.textAlignment = UITextAlignmentCenter;
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
self.label.text = @"正在在請(qǐng)求數(shù)據(jù)";
[self.view addSubview:label];
[label release];
//step 1:請(qǐng)求地址
NSString *urlString = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
//step 2:實(shí)例化一個(gè)request
NSURLRequest *requrst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
//step 3:創(chuàng)建鏈接
self.connection = [[NSURLConnection alloc] initWithRequest:requrst delegate:self];
if ( self.connection) {
NSLog(@"鏈接成功");
}else {
NSLog(@"鏈接失敗");
}
[url release];
[urlString release];
[requrst release];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
self.label = nil;
self.data = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark-
#pragma NSUrlConnectionDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//接受一個(gè)服務(wù)端回話,再次一般初始化接受數(shù)據(jù)的對(duì)象
NSLog(@"返回?cái)?shù)據(jù)類型:%@",[response textEncodingName]);
NSMutableData *d = [[NSMutableData alloc] init];
self.data = d;
[d release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//接受返回?cái)?shù)據(jù),這個(gè)方法可能會(huì)被調(diào)用多次,因此將多次返回?cái)?shù)據(jù)加起來(lái)
NSUInteger datalength = [data length];
NSLog(@"返回?cái)?shù)據(jù)量:%d",datalength);
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//連接結(jié)束
NSLog(@"%d:",[self.data length]);
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *mystr = [[NSString alloc] initWithData:_data encoding:enc];
// string i
NSLog(@"最后的結(jié)果:%@",mystr);
self.label.text = mystr;
[mystr release];
[self.connection release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//鏈接錯(cuò)誤
}
@end
簡(jiǎn)單說(shuō)下:
1.最簡(jiǎn)單的網(wǎng)絡(luò)鏈接,一個(gè)url,一個(gè)request,一個(gè)connection以及一個(gè)response返回。默認(rèn)的是get請(qǐng)求。
2.data轉(zhuǎn)碼問(wèn)題,這個(gè)一開始有點(diǎn)糾結(jié)。即,在最后我們要把NSData轉(zhuǎn)化成NSString時(shí)候需要一個(gè)轉(zhuǎn)碼格式,一開始我習(xí)慣性的用了UTF-8,
然后發(fā)現(xiàn)轉(zhuǎn)化后String 是Null,于是去打印了下請(qǐng)求返回的一些參數(shù),顯示的是GB2312~。