来自 MAF 应用程序的 Oracle MCS 自定义 API 调用
Oracle MCS Custom API call from MAF Application
我在 Oracle MCS 中创建了一个自定义 API 以获取用户信息并尝试从 MAF 应用程序调用它...作为响应,我得到 200 作为成功代码...但是当我尝试解析显示 HTML 页面而不是实际响应的响应....
自定义API
https://mobileportalsetrial1304dev-mcsdem0001.mobileenv.us2.oraclecloud.com:443/mobile/custom/rvs_ekkfetchuserinfo/fetchcontent
和 userid=101
作为参数
调用方法获取用户信息
@Override
public Response getUserInformation(int userId) {
System.out.println("In loginService");
String restURI = "https://mobileportalsetrial1304dev-mcsdem0001.mobileenv.us2.oraclecloud.com:443/mobile/custom/rvs_ekkfetchuserinfo/fetchcontent?userid=" + userId;
String jsonRequest = "";
Response response = new Response();
response = RestUtil.callGet(restURI, jsonRequest);
return response;
}
callGet 方法
public static Response callGet(String restURI, String jsonRequest) {
String responseJson = "";
Response response = new Response();
System.out.println("restURI:" + restURI);
RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
restServiceAdapter.clearRequestProperties();
restServiceAdapter.setConnectionName("MiddlewareAPI");
restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);
restServiceAdapter.addRequestProperty("Content-Type", "application/json");
restServiceAdapter.addRequestProperty("Accept", "application/json; charset=UTF-8");
restServiceAdapter.addRequestProperty("Oracle-Mobile-Backend-Id", "da5c7d86-29c0-43e8-b613-53de55a7ae6c");
restServiceAdapter.addRequestProperty("Authorization", "Basic TUNTREVNMDAwMV9NT0JJTEVQT1JUQUxTRVRSSUFMMTMwNERFVl9NT0JJTEVfQU5PTllNT1VTX0FQUElEOmR5Nm91NW5wX3RnbE5r");//+new String(encodedBytes));
restServiceAdapter.setRequestURI(restURI);
restServiceAdapter.setRetryLimit(0);
try {
responseJson = restServiceAdapter.send(jsonRequest);
System.out.println("response" + responseJson);
int responseCode = restServiceAdapter.getResponseStatus();
System.out.println("responseCode" + responseCode);
response.setResponseCode(responseCode);
response.setResponseMessage(responseJson);
response.setHeader(restServiceAdapter.getResponseHeaders());
} catch (Exception e) {
System.out.println("Error in calling API" + e.getStackTrace());
int responseCode = restServiceAdapter.getResponseStatus();
response.setResponseCode(responseCode);
response.setResponseMessage(responseJson);
}
return response;
}
Json 解析
JSONObject obj = new JSONObject(response);
JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
String user_id = arr.getJSONObject(i).getString("UserId");
}
这里我得到的是 JSONObject["items"] is not found... 当我打印响应消息时它给出了一个 HTML 脚本文件
预期输出
{
"items": [
{
"UserId": "101",
"AgentId": null,
"Category": "Rental",
"Division": "KDR",
"Status": null,
"LocationId": null,
"Operation": "CheckOut",
"Admin": "N",
"createdBy": "mcs-demo_user09@oracleads.com",
"createdOn": "2015-09-25T11:29:10.215564+00:00",
"modifiedBy": "mcs-demo_user09@oracleads.com",
"modifiedOn": "2015-09-25T11:29:10.215564+00:00"
}
]
}
HTML 页面的内容是什么(我想它会有一些 JavaScript 但也应该有一个 HTML 标题)。反正MCS里的user Id不是101而是内部ID,所以不知道你这道题有没有选101来简化。
在 MAF 中,REST 连接是通过具有根 URL 和相对 URI 的 REST 连接定义的。在您的示例中,REST 连接被引用为 "MiddlewareAPI"。除非此连接的值为空,否则您提供的 restURI 需要减少为不包含根 URL.
您收到的 HTTP 200 是因为服务器响应了请求。但是,它似乎缺少授权用户(如果访问 API 的基本授权失败),或者不允许经过身份验证的用户请求用户平台 API(默认情况下,您可以仅请求有关您重新验证身份的用户的信息。
弗兰克
嗨,我找到了解决方案...
我试图通过 Oracle-MCS 调用 customAPI。我用 HttpsURLConnection
替换了 RestServiceAdapter
。然后它工作得很好。
我在 Oracle MCS 中创建了一个自定义 API 以获取用户信息并尝试从 MAF 应用程序调用它...作为响应,我得到 200 作为成功代码...但是当我尝试解析显示 HTML 页面而不是实际响应的响应....
自定义API
https://mobileportalsetrial1304dev-mcsdem0001.mobileenv.us2.oraclecloud.com:443/mobile/custom/rvs_ekkfetchuserinfo/fetchcontent
和 userid=101
作为参数
调用方法获取用户信息
@Override
public Response getUserInformation(int userId) {
System.out.println("In loginService");
String restURI = "https://mobileportalsetrial1304dev-mcsdem0001.mobileenv.us2.oraclecloud.com:443/mobile/custom/rvs_ekkfetchuserinfo/fetchcontent?userid=" + userId;
String jsonRequest = "";
Response response = new Response();
response = RestUtil.callGet(restURI, jsonRequest);
return response;
}
callGet 方法
public static Response callGet(String restURI, String jsonRequest) {
String responseJson = "";
Response response = new Response();
System.out.println("restURI:" + restURI);
RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
restServiceAdapter.clearRequestProperties();
restServiceAdapter.setConnectionName("MiddlewareAPI");
restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);
restServiceAdapter.addRequestProperty("Content-Type", "application/json");
restServiceAdapter.addRequestProperty("Accept", "application/json; charset=UTF-8");
restServiceAdapter.addRequestProperty("Oracle-Mobile-Backend-Id", "da5c7d86-29c0-43e8-b613-53de55a7ae6c");
restServiceAdapter.addRequestProperty("Authorization", "Basic TUNTREVNMDAwMV9NT0JJTEVQT1JUQUxTRVRSSUFMMTMwNERFVl9NT0JJTEVfQU5PTllNT1VTX0FQUElEOmR5Nm91NW5wX3RnbE5r");//+new String(encodedBytes));
restServiceAdapter.setRequestURI(restURI);
restServiceAdapter.setRetryLimit(0);
try {
responseJson = restServiceAdapter.send(jsonRequest);
System.out.println("response" + responseJson);
int responseCode = restServiceAdapter.getResponseStatus();
System.out.println("responseCode" + responseCode);
response.setResponseCode(responseCode);
response.setResponseMessage(responseJson);
response.setHeader(restServiceAdapter.getResponseHeaders());
} catch (Exception e) {
System.out.println("Error in calling API" + e.getStackTrace());
int responseCode = restServiceAdapter.getResponseStatus();
response.setResponseCode(responseCode);
response.setResponseMessage(responseJson);
}
return response;
}
Json 解析
JSONObject obj = new JSONObject(response);
JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
String user_id = arr.getJSONObject(i).getString("UserId");
}
这里我得到的是 JSONObject["items"] is not found... 当我打印响应消息时它给出了一个 HTML 脚本文件
预期输出
{
"items": [
{
"UserId": "101",
"AgentId": null,
"Category": "Rental",
"Division": "KDR",
"Status": null,
"LocationId": null,
"Operation": "CheckOut",
"Admin": "N",
"createdBy": "mcs-demo_user09@oracleads.com",
"createdOn": "2015-09-25T11:29:10.215564+00:00",
"modifiedBy": "mcs-demo_user09@oracleads.com",
"modifiedOn": "2015-09-25T11:29:10.215564+00:00"
}
]
}
HTML 页面的内容是什么(我想它会有一些 JavaScript 但也应该有一个 HTML 标题)。反正MCS里的user Id不是101而是内部ID,所以不知道你这道题有没有选101来简化。
在 MAF 中,REST 连接是通过具有根 URL 和相对 URI 的 REST 连接定义的。在您的示例中,REST 连接被引用为 "MiddlewareAPI"。除非此连接的值为空,否则您提供的 restURI 需要减少为不包含根 URL.
您收到的 HTTP 200 是因为服务器响应了请求。但是,它似乎缺少授权用户(如果访问 API 的基本授权失败),或者不允许经过身份验证的用户请求用户平台 API(默认情况下,您可以仅请求有关您重新验证身份的用户的信息。
弗兰克
嗨,我找到了解决方案...
我试图通过 Oracle-MCS 调用 customAPI。我用 HttpsURLConnection
替换了 RestServiceAdapter
。然后它工作得很好。