Web Service(Chapter 28 of Cocoa Programming for Mac OS X)

本文介绍了一个使用Objective-C编写的AppController类,该类通过Amazon Web Services API进行图书搜索。代码展示了初始化过程、图书搜索功能的实现,包括URL构造、网络请求、XML解析及错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 #import "AppController.h"
 2 #define AWS_ID @"1CKE6MZ6S27EFQ458402"
 3 
 4 @implementation AppController
 5 
 6 - (id)init
 7 {
 8     self = [super init];
 9     if (self) {
10         // Initialization code here.
11     }
12     
13     return self;
14 }
15 
16 - (IBAction)fetchBooks:(id)sender
17 {
18     [progress startAnimation:nil];
19     
20     NSString *input = [searchField stringValue];
21     NSString *searchString = [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
22     NSLog(@"searchString = %@", searchString);
23     
24     NSString *urlString = [NSString stringWithFormat:
25                                                     @"http://ecs.amazonaws.com/onca/xml?"
26                                                     @"Service=AWSECommerceService&"
27                                                     @"AWSAcccessKeyID=%@&"
28                                                     @"Operation=ItemSearch&"
29                                                     @"SearchIndex=Books&"
30                                                     @"Keywords=%@&"
31                                                     @"Version=20007-07-16", AWS_ID, searchString];
32     NSURL *url = [NSURL URLWithString:urlString];
33     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
34     
35     NSData *urlData;
36     NSURLResponse *response;
37     NSError *error;
38     urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
39     
40     if(!urlData)
41     {
42         NSAlert *alert = [NSAlert alertWithError:error];
43         [alert runModal];
44         return;
45     }
46     
47     [doc release];
48     doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];
49     NSLog(@"doc = %@", doc);
50     if (!doc) 
51     {
52         NSAlert *alert = [NSAlert alertWithError:error];
53         [alert runModal];
54         return;
55     }
56     
57     [itemNodes release];
58     itemNodes = [[doc nodesForXPath:@"ItemSearchResponse/Items/Item" error:&error] retain];
59     if (!itemNodes) 
60     {
61         NSAlert *alert = [NSAlert alertWithError:error];
62         [alert runModal];
63         return;
64     }
65     
66     [tableView reloadData];
67     [progress stopAnimation:nil];
68 }
69 
70 - (int)numberOfRowsInTableView:(NSTableView*)tv
71 {
72     return 0;
73 }
74 
75 - (void)dealloc
76 {
77     [super dealloc];
78 }
79 
80 @end

转载于:https://www.cnblogs.com/zhtf2014/archive/2011/03/22/1991452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值