探索iPad应用中的Web服务与蓝牙编程
立即解锁
发布时间: 2025-08-24 01:10:18 阅读量: 1 订阅数: 3 

### 探索iPad应用中的Web服务与蓝牙编程
#### 1. Web服务基础
在进行Web服务通信时,有几个关键信息需要注意。Web服务的URL为 `http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml`,请求的 `Content-Type` 是 `application/x-www-form-urlencoded`,`Content-Length` 是 `V4IPAddress=string` 的长度。数据以键值对的形式发送,与HTTP GET不同,数据在HTTP头部之后发送,HTTP方法为POST。返回结果的格式如下:
```plaintext
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version=”1.0”?>
xml result
```
#### 2. 消费Web服务的方法比较
有三种方式可以消费Web服务,它们各有优缺点,如下表所示:
| 方法 | 优点 | 缺点 |
| ---- | ---- | ---- |
| HTTP GET | 简单,所有信息可通过查询字符串传递 | 查询字符串长度有限,不适合传递大量数据 |
| HTTP POST | 允许发送更多数据 | 数据需格式化为键值对,每个键值对大小限制为1024字符 |
| SOAP | 允许通过SOAP请求发送复杂数据类型 | - |
#### 3. 在iPad应用中使用SOAP消费Web服务
以下是使用SOAP方法在iPad应用中消费Web服务的详细步骤:
1. 使用Xcode创建一个基于视图的应用程序项目,命名为 `WebServices`。
2. 双击 `WebServicesViewController.xib` 文件,在Interface Builder中进行编辑。
3. 双击视图项,在视图窗口中添加以下视图:
- 标签
- 文本字段
- 圆角矩形按钮
- 活动指示器
4. 在Xcode中编辑 `WebServicesViewController.h` 文件,添加以下代码:
```objc
#import <UIKit/UIKit.h>
@interface WebServicesViewController : UIViewController {
IBOutlet UITextField *ipAddress;
IBOutlet UIActivityIndicatorView *activityIndicator;
NSMutableData *webData;
NSMutableString *soapResults;
NSURLConnection *conn;
}
@property (nonatomic, retain) UITextField *ipAddress;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
- (IBAction)buttonClicked:(id)sender;
@end
```
5. 保存文件并返回Interface Builder。
6. 执行以下操作:
- 按住Control键点击 `File’s Owner` 项,将其拖动到文本字段视图上,选择 `ipAddress`。
- 按住Control键点击 `File’s Owner` 项,将其拖动到活动指示器视图上,选择 `activityIndicator`。
- 按住Control键点击圆角矩形按钮视图,将其拖动到 `File’s Owner` 项上,选择 `buttonClicked:`。
7. 右键点击 `File’s Owner` 项,确认连接正确。
8. 在 `WebServicesViewController.m` 文件中添加以下代码:
```objc
import “WebServicesViewController.h”
@implementation WebServicesViewController
@synthesize ipAddress;
@synthesize activityIndicator;
```
9. 定义 `buttonClicked:` 方法,用于构建SOAP请求包并发送到Web服务:
```objc
- (IBAction)buttonClicked:(id)sender {
NSString *soapMsg =
[NSString stringWithFormat:
@“<?xml version=\“1.0\“ encoding=\“utf-8\“?>”
“<soap:Envelope xmlns:xsi=”
“\“http://www.w3.org/2001/XMLSchema-instance\“ “
“xmlns:xsd=\“http://www.w3.org/2001/XMLSchema\“ “
“xmlns:soap=\“http://schemas.xmlsoap.org/soap/envelope/\“>”
“<soap:Body>”
“<FindCountryAsXml xmlns=\“http://www.ecubicle.net/webservices/\“>”
“<V4IPAddress>%@</V4IPAddress>”
“</FindCountryAsXml>”
“</soap:Body>”
“</soap:Envelope>”, ipAddress.text
];
NSLog(soapMsg);
NSURL *url = [NSURL URLWithString:
@“http://www.ecubicle.net/iptocountry.asmx”];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@“%d”, [soapMsg length]];
[req addValue:@“text/xml; charset=utf-8” forHTTPHeaderField:@“Content-Type”];
[req addValue:@“http://www.ecubicle.net/webservices/FindCountryAsXml”
forHTTPHeaderField:@“SOAPAction”];
[req addValue:msgLength forHTTPHeaderField:@“Content-Length”];
[req setHTTPMethod:@“POST”];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
}
```
10. 在 `WebServicesViewController.m` 文件中添加以下方法:
```objc
-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection
didFailWithError:(NSError *) error {
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@“DONE. Received Bytes: %d”, [webData length]);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
[activityIndicator stopAnimating];
[connection release];
[webData release];
}
-(void) dealloc {
[ipAddress release];
[activityIndicator release];
[super dealloc];
}
```
11. 按下Command - R在iPhone模拟器上测试应用程序。在文本字段中输入IP地址 `34.5.6.7`,然后点击 `Find Country` 按钮。
12. 在Xcode中,按下Shift - Command - R打开调试控制台窗口,观察发送到Web服务的内容和Web服务的响应。
#### 4. 使用HTTP POST和HTTP GET与Web服务通信
- **HTTP POST**:只需修改 `buttonClicked:` 方法,代码如下:
```objc
- (IBAction)buttonClicked:(id)sender {
NSString *postString =
[NSString stringWithFormat:@“V4IPAddress=%@“, ipAddress.text];
NSLog(postString);
NSURL *url = [NSURL URLWithString:
@“http://www.ecubicle.net/iptocountry.asmx/FindCountryAsXml”];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@“%d”, [postString length]];
[req addValue:@“application/x-www-form-urlencoded”
forHTTPHeaderField:@“Content-Type”];
[req addValue:msgLength forHTTPHeaderField:@“Content-Length”];
[req setHTTPMethod:@“POST”];
[req setHTTPBody: [postString dataUsingEncoding:NSUTF8Str
```
0
0
复制全文
相关推荐









