woe

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

最近学习网络方面的知识,大部分都是用的时异步编程

以豆瓣为例子  

首先导入需要的框架和第三方库

#import "JSON.h" // 第三方解析json

#import "UIImageView+WebCache.h"// 异步加载图片

#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"

在实现文件中 

 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个步骤

 

posted on 2013-07-23 19:31  心火之源  阅读(211)  评论(0编辑  收藏  举报