来自 iOS 应用程序的资源请求
improve this page | report issue
概述
MobileFirst 应用程序可以使用 WLResourceRequest
REST API 访问资源。
REST API 将使用所有适配器和外部资源。
先决条件:
- 确保您已将 Mobile Foundation SDK 添加到本机 iOS 项目。
- 了解如何创建适配器。
WLResourceRequest
WLResourceRequest
类可处理对适配器或外部资源的资源请求。
创建 WLResourceRequest
对象并指定资源路径和 HTTP 方法。
可用方法包括:WLHttpMethodGet
、WLHttpMethodPost
、WLHttpMethodPut
和 WLHttpMethodDelete
。
Objective-C
WLResourceRequest *request = [WLResourceRequest requestWithURL:[NSURL URLWithString:@"/adapters/JavaAdapter/users/"] method:WLHttpMethodGet];
Swift
let request = WLResourceRequest(
URL: NSURL(string: "/adapters/JavaAdapter/users"),
method: WLHttpMethodGet
)
- 对于 JavaScript 适配器,请使用
/adapters/{AdapterName}/{procedureName}
- 对于 Java 适配器,请使用
/adapters/{AdapterName}/{path}
。path
取决于您如何在 Java 代码中定义@Path
注释。 这也将包含您使用的任何@PathParam
。 - 要访问项目外面的资源,请根据外部服务器的需求使用完整 URL。
- 超时:可选,请求超时(毫秒)
发送请求
使用 sendWithCompletionHandler
方法请求资源。
提供完成处理程序来处理检索到的数据:
Objective-C
[request sendWithCompletionHandler:^(WLResponse *response, NSError *error) {
if (error == nil){
NSLog(@"%@", response.responseText);
} else {
NSLog(@"%@", error.description);
}
}];
Swift
request.sendWithCompletionHandler { (response, error) -> Void in
if(error == nil){
NSLog(response.responseText)
}
else{
NSLog(error.description)
}
}
或者,您可以使用 sendWithDelegate
并提供遵守 NSURLConnectionDataDelegate
和 NSURLConnectionDelegate
协议的委派。 这将允许您处理具有更高粒度的响应,如处理二进制响应。
参数
在发送请求之前,您可能希望根据需要添加参数。
路径参数
如上所述,在创建 WLResourceRequest
对象期间设置路径参数 (/path/value1/value2
)。
查询参数
要发送查询参数 (/path?param1=value1...
),请对每个参数使用 setQueryParameter
方法:
Objective-C
[request setQueryParameterValue:@"value1" forName:@"param1"];
[request setQueryParameterValue:@"value2" forName:@"param2"];
Swift
request.setQueryParameterValue("value1", forName: "param1")
request.setQueryParameterValue("value2", forName: "param2")
JavaScript 适配器
JavaScript 适配器使用有序的无名参数。 要将参数传递到 JavaScript 适配器,请设置名称为 params
的参数数组:
Objective-C
[request setQueryParameterValue:@"['value1', 'value2']" forName:@"params"];
Swift
request.setQueryParameterValue("['value1', 'value2']", forName: "params")
这应该与 WLHttpMethodGet
一起使用。
表单参数
要发送主体中的表单参数,请使用 sendWithFormParameters
代替 sendWithCompletionHandler
:
Objective-C
//@FormParam("height")
NSDictionary *formParams = @{@"height":@"175"};
//Sending the request with Form parameters
[request sendWithFormParameters:formParams completionHandler:^(WLResponse *response, NSError *error) {
if (error == nil){
NSLog(@"%@", response.responseText);
} else {
NSLog(@"%@", error.description);
}
}];
Swift
//@FormParam("height")
let formParams = ["height":"175"]
//Sending the request with Form parameters
request.sendWithFormParameters(formParams) { (response, error) -> Void in
if(error == nil){
NSLog(response.responseText)
}
else{
NSLog(error.description)
}
}
JavaScript 适配器
JavaScript 适配器使用有序的无名参数。 要将参数传递到 JavaScript 适配器,请设置名称为 params
的参数数组:
Objective-C
NSDictionary *formParams = @{@"params":@"['value1', 'value2']"};
Swift
let formParams = ["params":"['value1', 'value2']"]
这应该与 WLHttpMethodPost
一起使用。
头参数
要将参数作为 HTTP 头发送,请使用 setHeaderValue
API:
Objective-C
//@HeaderParam("Date")
[request setHeaderValue:@"2015-06-06" forName:@"birthdate"];
Swift
//@HeaderParam("Date")
request.setHeaderValue("2015-06-06", forName: "birthdate")
其他定制主体参数
sendWithBody
允许您在主体中设置任意字符串。sendWithJSON
允许您在主体中设置任意字典。sendWithData
允许您在主体中设置任意NSData
。
completionHandler 和 delegate 回调队列
为了避免在接收响应时阻止 UI,可以指定专用回调队列,执行 API 的 sendWithCompletionHandler
和 sendWithDelegate
集的 completionHandler 块和 delegate。
Objective-C
//creating callback queue
dispatch_queue_t completionQueue = dispatch_queue_create("com.ibm.mfp.app.callbackQueue", DISPATCH_QUEUE_SERIAL);
//Sending the request with callback queue
[request sendWithCompletionHandler:completionQueue completionHandler:^(WLResponse *response, NSError *error) {
if (error == nil){
NSLog(@"%@", response.responseText);
} else {
NSLog(@"%@", error.description);
}
}];
Swift
//creating callback queue
var completionQueue = dispatch_queue_create("com.ibm.mfp.app.callbackQueue", DISPATCH_QUEUE_SERIAL)
//Sending the request with callback queue
request.sendWithCompletionHandler(completionQueue) { (response, error) -> Void in
if (error == nil){
NSLog(@"%@", response.responseText);
} else {
NSLog(@"%@", error.description);
}
}
响应
response
对象包含响应数据,并且您可以使用其方法和属性来检索必需信息。 常用属性包括:responseText
(字符串)、responseJSON
(字典)(如果以 JSON 格式响应)和 status
(整数)(响应的 HTTP 状态)。
使用 response
和 error
对象获取从适配器检索的数据。
获取更多信息
有关 WLResourceRequest 的更多信息,请参阅 API 参考。
样本应用程序
ResourceRequestSwift 项目包含使用 Swift 实现的 iOS 应用程序,该应用程序使用 Java 适配器发出资源请求。
适配器 Maven 项目包含在资源请求调用期间使用的 Java 适配器。
单击以下载 iOS 项目。
单击以下载适配器 Maven 项目。
样本用法
遵循样本的 README.md 文件以获取指示信息。
关于 iOS 9 的注意事项:
▲Xcode 7 缺省情况下会启用应用程序传输安全性 (ATS)。 要完成教程,请禁用 ATS(阅读更多)。
- 在 Xcode 中,右键单击 [project]/info.plist 文件 → 打开方式 → 源代码
- 粘贴以下内容:
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.