最近學習網絡方面的知識,大部分都是用的時異步編程node
以豆瓣爲例子 ios
首先導入須要的框架和第三方庫編程
#import "JSON.h" // 第三方解析jsonjson
#import "UIImageView+WebCache.h"// 異步加載圖片api
#import "GDataXMLNode.h"// 第三方解析xml數組
//douban網址緩存
#define SERVICEADD @"http://api.douban.com/book/subjects?q=ios&start-index=1&max-%20results=10&apikey=0f56d0e8157561512e0472b8406023f3&alt=json"網絡
在實現文件中 app
1 //建立合法的url 2 NSURL *myUrl = [NSURL URLWithString:SERVICEADD]; 3 //建立請求 4 NSURLRequest *myRequest = [[[NSURLRequest alloc]initWithURL:myUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30] autorelease]; 5 6 NSURLConnection *myConnection = [[NSURLConnection alloc]initWithRequest:myRequest delegate:self startImmediately:YES]; 7 8 9 而後是網絡delegate的三個方法 10 //異步下載的3不曲 11 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 12 //get response 13 NSHTTPURLResponse *myResponse = (NSHTTPURLResponse *)response; 14 NSLog(@"%d",[myResponse statusCode]);//狀態碼 15 16 //清空緩存區 17 [_data setLength:0]; 18 19 } 20 21 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 22 //get data 23 [_data appendData:data]; 24 25 } 26 27 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 28 //connect complete 29 if (isRefresh) { 30 [bookArr removeAllObjects]; 31 } 32 /* 33 //開始解析 xml解析 34 GDataXMLDocument *root = [[GDataXMLDocument alloc]initWithData:_data options:0 error:nil]; 35 if (root == nil) { 36 return; 37 } 38 39 //加入namespace 字典打開xml 40 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.w3.org/2005/Atom",@"xmlns", nil]; 41 42 //得到字節點數組 43 NSArray *elementArr = [root nodesForXPath:@"/xmlns:feed/xmlns:entry" namespaces:dict error:nil]; 44 // NSLog(@"%@",elementArr); 45 46 //遍歷元素數組 47 for (GDataXMLElement *element in elementArr) { 48 //建立本身的數據類 49 BookItem *oneBook = [[BookItem alloc]init]; 50 //書名 51 oneBook.bookName = [[[element elementsForName:@"title"] lastObject] stringValue]; 52 oneBook.bookAuthor = [[[[[element elementsForName:@"author"] lastObject] elementsForName:@"name"] lastObject] stringValue]; 53 54 //獲取圖片連接 55 NSArray *linkArr = [element elementsForName:@"link"]; 56 for (GDataXMLElement *subElelment in linkArr) { 57 58 GDataXMLNode *oneNode = [subElelment attributeForName:@"rel"]; 59 if ([[oneNode stringValue] isEqualToString:@"image"]) { 60 GDataXMLNode *href = [subElelment attributeForName:@"href"]; 61 oneBook.bookImage = href.stringValue; 62 break; 63 } 64 65 } 66 [bookArr addObject:oneBook]; 67 [oneBook release]; 68 69 } 70 */ 71 72 //開始解析數據 json解析 73 NSString *datastring = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding]; 74 NSDictionary *dic = [datastring JSONValue]; 75 [datastring release]; 76 //NSLog(@"%@",dic); 77 NSArray *subArr = [dic objectForKey:@"entry"]; 78 for (NSDictionary *subDic in subArr) { 79 BookItem *oneBook = [[BookItem alloc]init]; 80 oneBook.bookName = [[subDic objectForKey:@"title"] objectForKey:@"$t"]; 81 oneBook.bookAuthor = [[[[subDic objectForKey:@"author"] objectAtIndex:0] objectForKey:@"name"]objectForKey:@"$t"]; 82 oneBook.bookImage = [[[subDic objectForKey:@"link"] objectAtIndex:2] objectForKey:@"@href"]; 83 NSLog(@"%@",oneBook.bookImage); 84 [bookArr addObject:oneBook]; 85 [oneBook release]; 86 } 87 88 89 UITableView *myTable = (UITableView *)[self.view viewWithTag:1010]; 90 [myTable reloadData]; 91 92 [self loadingFinished]; 93 [connection release]; 94 95 } ps:裏面有些變量沒有聲明 主要是紀錄 xml 和json解析 以及異步編程的3個步驟