简体   繁体   中英

Oracle MCS Custom API call from MAF Application

I have created a custom API in Oracle MCS to get the user information and trying to call it from MAF application... As a response i am getting 200 as success code... but when i try to parse the response it shows a HTML page instead of actual responce....

Custom API

https://mobileportalsetrial1304dev-mcsdem0001.mobileenv.us2.oraclecloud.com:443/mobile/custom/rvs_ekkfetchuserinfo/fetchcontent and userid=101 as parameter

Calling Method to get User information

    @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 Method

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 Parsing

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");
}

Here what i am getting is JSONObject["items"] is not found ... when i print the responce message it gives a HTML Script file

Expected Output

{
    "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"
        }
    ]
}

what is the content of the HTML page (it will have some JavaScript I assume but should have a HTML title as well). Anyway, a user Id in MCS is not 101 but an internal ID, so I don't know if you've chosen 101 for simplification in this question.

In MAF, the REST connection is defined through a REST connection with the root URL and the relative URI. In your example, the REST connection is referenced as "MiddlewareAPI". Unless the value of this connection is null, the restURI you provide needs to be reduced to not contain the root URL.

The HTTP 200 you get because the request is answered by the server. However, it appears to be missing either an authorized user (in case of a failed basic authorization for accessing the API) or the authenticated user is not allowed to request the user platform API (by default you can only request information about the user you re authenticated as).

Frank

Hi I got the solution... I was trying to call customAPI through Oracle-MCS. I replaced RestServiceAdapter with HttpsURLConnection . Then it Works perfectly fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM