IBM MobileFirst 7.1 - 从 Android 客户端发出请求时出错
IBM MobileFirst 7.1 - Error when making request from Android Client
在调用 MFP 适配器时,我收到此响应
{
"statusCode":400,
"errors":[
],
"isSuccessful":true,
"statusReason":"Bad Request",
"html":{
"body":"",
"html":"http://www.w3.org/1999/xhtml",
"xmlns":"http://www.w3.org/1999/xhtml"
},
"responseHeaders":{
"Content-Language":"en-US",
"Date":"Thu, 11 Feb 2016 14:47:03 GMT",
"Content-Length":"0",
"Expires":"Thu, 01 Dec 1994 16:00:00 GMT",
"Set-Cookie":[
"JSESSIONID=0000e8OwOu5nRy5GHcnq5aOyJ6W:-1; Path=/; HttpOnly",
"JSESSIONID=0000kr3JjAeXlHQyeCFsWm9Sagx:-1; Path=/; HttpOnly"
],
"Connection":"Close",
"X-Powered-By":"Servlet/3.0",
"Cache-Control":"no-cache=\"set-cookie, set-cookie2\""
},
"warnings":[
],
"totalTime":200,
"responseTime":198,
"authRequired":true,
"info":[
]
}
事实是,当我通过浏览器向我的 websphere 服务器发出相同的请求,或者直接从 eclipse 调用适配器时,我得到了预期的响应。
相关Android代码
调用MFP服务器
public static void validateUserId(MobileFirstActivity activity, String userId, boolean saveUserId,
WLResponseListener listener) {
ValidateUserIdRequest validateUserIdRequest = new ValidateUserIdRequest(userId, saveUserId);
activity.performResourceRequest(validateUserIdRequest, listener);
}
ValidateUserIdRequest 对象
class ValidateUserIdRequest implements ResourceRequest {
private final String userId;
private static final String appVersion = BuildConfig.VERSION_NAME;
private final String saveUserId;
private static final String devicePrint = "Android";
public ValidateUserIdRequest(String userId, boolean saveUserId) {
this.userId = userId;
this.saveUserId = String.valueOf(saveUserId);
}
@Override
public int setEndpointResource() {
return R.string.ValidateUserId;
}
@Override
public void setParams(WLResourceRequest resourceRequest) {
resourceRequest.setQueryParameter("params",
ResourceUtils.convertMapToParamString(
userId, appVersion, saveUserId, devicePrint));
}
@Override
public String setRequestMethod() {
return WLResourceRequest.GET;
}
实际执行请求的方法
public void performResourceRequest(final ResourceRequest resourceRequest, final WLResponseListener listener) {
if (!isConnectedToMfpServer) {
connectToMfpServer(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
isConnectedToMfpServer = true;
performResourceRequest(resourceRequest, listener);
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
isConnectedToMfpServer = false;
listener.onFailure(wlFailResponse);
}
});
} else {
try {
URI endpoint = new URI(getString(resourceRequest.setEndpointResource()));
WLResourceRequest request = new WLResourceRequest(endpoint, resourceRequest.setRequestMethod());
resourceRequest.setParams(request);
request.send(listener);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
将 String[] 参数转换为兼容字符串的实用方法。
public static String convertMapToParamString(String... params) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = 0; i < params.length; i++) {
stringBuilder.append("'").append(params[i]).append("'");
if (i != (params.length - 1)) {
stringBuilder.append(",");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
convertMapToParamString 结果给了我这个 as per the documentation for Javascript based adapters
['mbltest3','1.0','false','Android']
我在 MFP 服务器上获得了正确的参数,但请求仍然错误。
我还能尝试什么?
原来我的请求的 cookie 没有正确附加导致 400 错误。
在调用 MFP 适配器时,我收到此响应
{
"statusCode":400,
"errors":[
],
"isSuccessful":true,
"statusReason":"Bad Request",
"html":{
"body":"",
"html":"http://www.w3.org/1999/xhtml",
"xmlns":"http://www.w3.org/1999/xhtml"
},
"responseHeaders":{
"Content-Language":"en-US",
"Date":"Thu, 11 Feb 2016 14:47:03 GMT",
"Content-Length":"0",
"Expires":"Thu, 01 Dec 1994 16:00:00 GMT",
"Set-Cookie":[
"JSESSIONID=0000e8OwOu5nRy5GHcnq5aOyJ6W:-1; Path=/; HttpOnly",
"JSESSIONID=0000kr3JjAeXlHQyeCFsWm9Sagx:-1; Path=/; HttpOnly"
],
"Connection":"Close",
"X-Powered-By":"Servlet/3.0",
"Cache-Control":"no-cache=\"set-cookie, set-cookie2\""
},
"warnings":[
],
"totalTime":200,
"responseTime":198,
"authRequired":true,
"info":[
]
}
事实是,当我通过浏览器向我的 websphere 服务器发出相同的请求,或者直接从 eclipse 调用适配器时,我得到了预期的响应。
相关Android代码
调用MFP服务器
public static void validateUserId(MobileFirstActivity activity, String userId, boolean saveUserId,
WLResponseListener listener) {
ValidateUserIdRequest validateUserIdRequest = new ValidateUserIdRequest(userId, saveUserId);
activity.performResourceRequest(validateUserIdRequest, listener);
}
ValidateUserIdRequest 对象
class ValidateUserIdRequest implements ResourceRequest {
private final String userId;
private static final String appVersion = BuildConfig.VERSION_NAME;
private final String saveUserId;
private static final String devicePrint = "Android";
public ValidateUserIdRequest(String userId, boolean saveUserId) {
this.userId = userId;
this.saveUserId = String.valueOf(saveUserId);
}
@Override
public int setEndpointResource() {
return R.string.ValidateUserId;
}
@Override
public void setParams(WLResourceRequest resourceRequest) {
resourceRequest.setQueryParameter("params",
ResourceUtils.convertMapToParamString(
userId, appVersion, saveUserId, devicePrint));
}
@Override
public String setRequestMethod() {
return WLResourceRequest.GET;
}
实际执行请求的方法
public void performResourceRequest(final ResourceRequest resourceRequest, final WLResponseListener listener) {
if (!isConnectedToMfpServer) {
connectToMfpServer(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
isConnectedToMfpServer = true;
performResourceRequest(resourceRequest, listener);
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
isConnectedToMfpServer = false;
listener.onFailure(wlFailResponse);
}
});
} else {
try {
URI endpoint = new URI(getString(resourceRequest.setEndpointResource()));
WLResourceRequest request = new WLResourceRequest(endpoint, resourceRequest.setRequestMethod());
resourceRequest.setParams(request);
request.send(listener);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
将 String[] 参数转换为兼容字符串的实用方法。
public static String convertMapToParamString(String... params) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = 0; i < params.length; i++) {
stringBuilder.append("'").append(params[i]).append("'");
if (i != (params.length - 1)) {
stringBuilder.append(",");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
convertMapToParamString 结果给了我这个 as per the documentation for Javascript based adapters
['mbltest3','1.0','false','Android']
我在 MFP 服务器上获得了正确的参数,但请求仍然错误。 我还能尝试什么?
原来我的请求的 cookie 没有正确附加导致 400 错误。