네트워크(웹서비스연결 xml파싱) pdf

11

Click here to load reader

Upload: sunwooindia

Post on 10-May-2015

1.180 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 네트워크(웹서비스연결 Xml파싱) pdf

웹서비스송진석

2011년 10월 2일 일요일

Page 2: 네트워크(웹서비스연결 Xml파싱) pdf

웹서비스 및 데이타 파싱

• 웹서비스 서버는 네트워크 사용자가 특정 웹서비스를 요청하면 XML이나 Jason화일 형태로 서비스를 제공

• XML은 element 태크를 이용하여 정보를 제공

• <element> 데이타 </element>형태로 데이타제공

• IOS는 NSURL, NSURLRequest, URLConnection을 이용하여 웹서비스 서버에 연결하여 XML데이타 수신

• 수신된 XML데이타는 NSXMLParser를 이용하여 데이타 분리

2011년 10월 2일 일요일

Page 3: 네트워크(웹서비스연결 Xml파싱) pdf

Window-Based Application 선택프로젝트 이름 TopSongs

UIViewController의 서브클래스RSSTableViewController 생성

XIB화일은 만들지 않음

2011년 10월 2일 일요일

Page 4: 네트워크(웹서비스연결 Xml파싱) pdf

#import <UIKit/UIKit.h>

@interface TopSongsAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

#import "TopSongsAppDelegate.h"#import "RSSTableViewController.h"

@implementation TopSongsAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // Override point for customization after application launch. RSSTableViewController *tvc = [[[RSSTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease]; [self.window setRootViewController:tvc]; [self.window makeKeyAndVisible]; return YES;}

두라인 입력2011년 10월 2일 일요일

Page 5: 네트워크(웹서비스연결 Xml파싱) pdf

#import <UIKit/UIKit.h>

@interface RSSTableViewController : UITableViewController <NSXMLParserDelegate> { BOOL waitingForEntryTitle; NSMutableString *titleString; NSMutableArray *songs; NSMutableData *xmlData; NSURLConnection *connectionInProgress; }

- (void) loadSongs;

@end

XML화일에서 title element내의 텍스트를 저장

XML화일을 저장하기 위한 데이타 변수

웹서비스 서버에 대한 코넥션

톱텐 Song List

헤더내의 title element와 entry내의 title element를 구분하기 위한 flag

2011년 10월 2일 일요일

Page 6: 네트워크(웹서비스연결 Xml파싱) pdf

-(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@"called in viewWillApear");

[self loadSongs];}

TableView가 화면에 뿌려지면 실행됨

- (void) loadSongs{ [songs removeAllObjects]; [[self tableView] reloadData]; NSURL *url = [NSURL URLWithString:@"http://ax.itunes.apple.com/" @"WebObjects/MZStoreServices.woa/ws/RSS/topsongs/" @"limit=10/xml"];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; if(!connectionInProgress) { [connectionInProgress cancel]; [connectionInProgress release]; [xmlData release]; xmlData = [[NSMutableData alloc] init]; NSLog(@"called in loadSongs");

connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; } }

웹서비스 베이스 어드레스 웹서비스 응용프로그램 인수

웹코넥션 연결 및 델리게이트 설정

2011년 10월 2일 일요일

Page 7: 네트워크(웹서비스연결 Xml파싱) pdf

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"called in didreceived"); [xmlData appendData:data];}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"xmlCheck = %@", xmlCheck); NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData]; [parser setDelegate:self]; [parser parse]; [parser release]; [[self tableView] reloadData];}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ [connectionInProgress release]; connectionInProgress = nil; [xmlData release]; xmlData = nil; NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]]; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString delegate:nil cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil]; [actionSheet showInView:[[self view] window]]; [actionSheet autorelease];}

데이타 수신완료후 호출

데이타 일부가 수신될때마다 호출

XMLParse생성 및 받은 XML데이타로 초기화XML데이타 파싱을 개시

코넥션에러시 호출됨

XML데이타 파싱을 완료후 songs 에레이의 내용을 테이블뷰에 채움

2011년 10월 2일 일요일

Page 8: 네트워크(웹서비스연결 Xml파싱) pdf

XML파싱델리게이트 메소드

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if([elementName isEqual:@"entry"]) { NSLog(@"Found a song entry"); waitingForEntryTitle = YES; } if([elementName isEqual:@"title"] && waitingForEntryTitle) { NSLog(@"found title!"); titleString = [[NSMutableString alloc] init]; }}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ [titleString appendString:string];}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if([elementName isEqual:@"title"] && waitingForEntryTitle) { NSLog(@"ended title:%@", titleString); [songs addObject:titleString]; [titleString release]; titleString = nil; } if([elementName isEqual:@"entry"]) { NSLog(@"ended a song entry"); waitingForEntryTitle = NO; }}

element tag가 읽혀 질때 마다 호출됨

element tag(<title> string </title>내의 캐릭터를 일일이 읽을때 호출됨

element tag(<title> string </title>내의 캐릭터를 다읽은후 호출됨

2011년 10월 2일 일요일

Page 9: 네트워크(웹서비스연결 Xml파싱) pdf

TableView DataSource Method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [songs count];}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease]; } [[cell textLabel] setText:[songs objectAtIndex:[indexPath row]]]; return cell;}

2011년 10월 2일 일요일

Page 10: 네트워크(웹서비스연결 Xml파싱) pdf

xmlCheck = <?xml version="1.0" encoding="utf-8"?>

! <feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">! ! <id>http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml</id><title>iTunes Store: Top Songs</title><updated>2011-09-30T14:43:55-07:00</updated><link rel="alternate" type="text/html" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?id=38&amp;popId=1"/><link rel="self" href="http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml"/><icon>http://phobos.apple.com/favicon.ico</icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></author><rights>Copyright 2008 Apple Inc.</rights>

2011년 10월 2일 일요일

Page 11: 네트워크(웹서비스연결 Xml파싱) pdf

<entry>! ! ! ! <updated>2011-09-30T14:43:55-07:00</updated>! ! ! !! ! ! ! ! <id>http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&amp;uo=2</id>! ! ! !! ! ! ! ! <title>Someone Like You - ADELE</title>! ! ! !! ! ! ! !! ! ! !! ! ! ! ! <im:name>Someone Like You</im:name>! ! ! !! ! ! ! ! <link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&amp;uo=2"/>! ! ! !! ! ! ! ! <im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType>! ! ! !! ! ! ! ! <category term="Pop" scheme="http://itunes.apple.com/us/genre/music-pop/id14?uo=2" label="Pop"/>! ! ! !! ! ! ! ! <link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a1.mzstatic.com/us/r1000/051/Music/71/b2/95/mzi.rsrrzevf.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>! ! ! !! ! ! ! ! <im:artist href="http://itunes.apple.com/us/artist/adele/id262836961?uo=2">ADELE</im:artist>! ! ! !! ! ! ! ! <im:price amount="1.29000" currency="USD">$1.29</im:price>! ! ! !! ! ! ! ! <im:image height="55">http://a1.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.55x55-70.jpg</im:image>! ! ! !! ! ! ! ! <im:image height="60">http://a5.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.60x60-50.jpg</im:image>! ! ! !! ! ! ! ! <im:image height="170">http://a4.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.170x170-75.jpg</im:image>! ! ! !! ! ! ! ! <rights>2010 XL Recordings Ltd</rights>! ! ! !! ! ! ! ! <im:releaseDate label="February 22, 2011">2011-02-22T00:00:00-07:00</im:releaseDate>! ! ! !! ! ! ! ! <im:collection><im:name>21</im:name><link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/21/id420075073?uo=2"/><im:contentType term="Music" label="Music"><im:contentType term="Album" label="Album"/></im:contentType></im:collection>! ! ! !! ! ! !! ! ! ! ! <content type="html">&lt;table border=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;tr&gt; &lt;td&gt; &lt;table border=&quot;0&quot; width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt; &lt;tr valign=&quot;top&quot; align=&quot;left&quot;&gt; &lt;td align=&quot;center&quot; width=&quot;166&quot; valign=&quot;top&quot;&gt; &lt;a href=&quot;http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&amp;uo=2&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;Someone Like You artwork&quot; src=&quot;http://a4.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.170x170-75.jpg&quot; /&gt;&lt;/a&gt; &lt;/td&gt; &lt;td width=&quot;10&quot;&gt;&lt;img alt=&quot;&quot; width=&quot;10&quot; height=&quot;1&quot; src=&quot;http://r.mzstatic.com/images/spacer.gif&quot; /&gt;&lt;/td&gt; !&lt;td width=&quot;95%&quot;&gt; &lt;b&gt;&lt;a href=&quot;http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&amp;uo=2&quot;&gt;Someone Like You&lt;/a&gt;&lt;/b&gt;&lt;br/&gt; &lt;a href=&quot;http://itunes.apple.com/us/album/21/id420075073?uo=2&quot;&gt;21&lt;/a&gt;&lt;br/&gt;

&lt;a href=&quot;http://itunes.apple.com/us/artist/adele/id262836961?uo=2&quot;&gt;ADELE&lt;/a&gt;

&lt;font size=&quot;2&quot; face=&quot;Helvetica,Arial,Geneva,Swiss,SunSans-Regular&quot;&gt;! ! ! ! ! !! ! ! ! ! ! ! &lt;br/&gt; &lt;b&gt;Genre:&lt;/b&gt; &lt;a href=&quot;http://itunes.apple.com/us/genre/music-pop/id14?uo=2&quot;&gt;Pop&lt;/a&gt; ! ! ! ! ! ! ! &lt;br/&gt; &lt;b&gt;Price:&lt;/b&gt; $1.29 ! ! ! ! ! ! ! &lt;br/&gt; &lt;b&gt;Release Date:&lt;/b&gt; February 22, 2011 &lt;/font&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;font size=&quot;2&quot; face=&quot;Helvetica,Arial,Geneva,Swiss,SunSans-Regular&quot;&gt; &amp;#169; 2010 XL Recordings Ltd&lt;/font&gt;! &lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;</content>! ! ! !! ! ! </entry>

2011년 10월 2일 일요일