IBM MobileFirst 将 JSON Body 从 iOS SDK 发送到 Java 适配器
IBM MobileFirst Sending JSON Body from iOS SDK to Java Adapter
使用 IBM Mobile First PlatForm 版本 7.1 我正在尝试使用以下代码从 iOS 应用程序调用 Java 适配器。
[[WLResourceRequest requestWithURL:url method:WLHttpMethodPost] sendWithJSON:@{@"one":@"two"} completionHandler:^(WLResponse *response, NSError *error) {
NSLog(@"%@",response);
NSLog(@"%@",error);
}];
Java适配器端的方法如下所示。
@POST
@Consumes("application/json")
@Produces("application/json")
public String hello(JSONObject body){
return body.toString();
}
但是我收到以下响应错误
2016-04-20 11:31:15.520 mbs-call[15092:3787968] Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response= { URL: http:/0.0.0.0:10080/mbs-api/adapters/basicadpt/users } { status code: 415, headers {
Connection = Close;
"Content-Language" = "en-US";
"Content-Length" = 0;
Date = "Wed, 20 Apr 2016 02:31:15 GMT";
"X-Powered-By" = "Servlet/3.0";
} }, NSErrorFailingURLKey=http://0.0.0.0:10080/mbs-api/adapters/basicadpt/users, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: unsupported media type (415)}
并且似乎在 iOS SDK 中它在调用任何方法时在请求中添加 header application/x-www-url-form-urlencoded
。
我有以下两个问题。
- 如何将 JSON Body 传递给 Java 适配器?
sendWithJSON
的行为在 iOS 和 Android 上不同。在 Android 上,当我们调用适配器时,它似乎添加了 application/json
header。这是错误还是部分行为?
我认为这是一个错误。我认为使用 sendWithJSON
应该自动假定内容类型是 application/json
.
我建议您打开支持请求 (PMR),以便他们改进体验。
同时,我找到了一个简单的解决方法:
[request addHeaderValue:@"application/json" forName:@"Content-Type"]
或在Swift:
request.addHeaderValue("application/json", forName: "Content-Type")
我在应用程序的 cordova 版本中遇到了同样的问题。
var userIDTag = 'some_string';
var subTag = [userIDTag]; //<- this worked
var subTag = userIDTag; //<- this failed with the above error
var subTag = '[\'' + some_string + '\']'; //<- this also failed with the above error
以下是我最终为 Cordova 应用所做的工作。
function subscribeByTag(userIDTag) {
var subTag = [userIDTag];
console.log("subTag: " + subTag);
WLAuthorizationManager.obtainAccessToken("push.mobileclient").then(
MFPPush.subscribe(
subTag,
function(subTag) {
navigator.notification.alert("Subscribed successfully");
},
function(failureResponse){
navigator.notification.alert("Failed to subscribe");
console.log("Failedtosubscribe:" + JSON.stringify(failureResponse));
}
)
);
}
使用 IBM Mobile First PlatForm 版本 7.1 我正在尝试使用以下代码从 iOS 应用程序调用 Java 适配器。
[[WLResourceRequest requestWithURL:url method:WLHttpMethodPost] sendWithJSON:@{@"one":@"two"} completionHandler:^(WLResponse *response, NSError *error) {
NSLog(@"%@",response);
NSLog(@"%@",error);
}];
Java适配器端的方法如下所示。
@POST
@Consumes("application/json")
@Produces("application/json")
public String hello(JSONObject body){
return body.toString();
}
但是我收到以下响应错误
2016-04-20 11:31:15.520 mbs-call[15092:3787968] Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" UserInfo={com.alamofire.serialization.response.error.response= { URL: http:/0.0.0.0:10080/mbs-api/adapters/basicadpt/users } { status code: 415, headers { Connection = Close; "Content-Language" = "en-US"; "Content-Length" = 0; Date = "Wed, 20 Apr 2016 02:31:15 GMT"; "X-Powered-By" = "Servlet/3.0"; } }, NSErrorFailingURLKey=http://0.0.0.0:10080/mbs-api/adapters/basicadpt/users, com.alamofire.serialization.response.error.data=<>, NSLocalizedDescription=Request failed: unsupported media type (415)}
并且似乎在 iOS SDK 中它在调用任何方法时在请求中添加 header application/x-www-url-form-urlencoded
。
我有以下两个问题。
- 如何将 JSON Body 传递给 Java 适配器?
sendWithJSON
的行为在 iOS 和 Android 上不同。在 Android 上,当我们调用适配器时,它似乎添加了application/json
header。这是错误还是部分行为?
我认为这是一个错误。我认为使用 sendWithJSON
应该自动假定内容类型是 application/json
.
我建议您打开支持请求 (PMR),以便他们改进体验。
同时,我找到了一个简单的解决方法:
[request addHeaderValue:@"application/json" forName:@"Content-Type"]
或在Swift:
request.addHeaderValue("application/json", forName: "Content-Type")
我在应用程序的 cordova 版本中遇到了同样的问题。
var userIDTag = 'some_string';
var subTag = [userIDTag]; //<- this worked
var subTag = userIDTag; //<- this failed with the above error
var subTag = '[\'' + some_string + '\']'; //<- this also failed with the above error
以下是我最终为 Cordova 应用所做的工作。
function subscribeByTag(userIDTag) {
var subTag = [userIDTag];
console.log("subTag: " + subTag);
WLAuthorizationManager.obtainAccessToken("push.mobileclient").then(
MFPPush.subscribe(
subTag,
function(subTag) {
navigator.notification.alert("Subscribed successfully");
},
function(failureResponse){
navigator.notification.alert("Failed to subscribe");
console.log("Failedtosubscribe:" + JSON.stringify(failureResponse));
}
)
);
}