IBM MobileFirst 7.1 - 从Android客户端

时间:2016-02-11 15:00:24

标签: java android ibm-mobilefirst mobilefirst-adapters mobilefirst-server

拨打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服务器上获得了正确的参数,但请求仍然很糟糕。 我还能尝试什么?

1 个答案:

答案 0 :(得分:0)

原来我的请求的cookie未正确附加导致400错误。