Tuesday, December 22, 2015

Oracle MAF: Programmatically call REST service ( REST with JSON output)

This example is using RESTServicesAdapter. Using that programmatically you can call a REST service and you can manipulate response as per need.

My REST service details are as follows:

URL: localhost:1111/HrServiceFirst-ViewController-context-root/resources/view/emp

{"lstEmp":[{"address":"ss","empId":22222,"empName":"sss"},{"address":"SKING","empId":100,"empName":"Steven"},{"address":"NKOCHHAR","empId":101,"empName":"Neena"},{"address":"LDEHAAN","empId":102,"empName":"Lex"},{"address":"AHUNOLD","empId":103,"empName":"Alexander"}]}

Step 1: Create new Mobile Application Framework (MAF) Application:



Step 2 :Right click on Connenction create new REST Connection

Give the name of rest Connection and URL End point click ok as follow:

Step 3: Create Employee Class and add following variable and generate accessors

    public Integer empId;
    public String empName;
    public String address;


Step 4: create  CallService class and add follwing code in it

package mobile;

import java.util.ArrayList;
import java.util.List;

import oracle.adfmf.dc.ws.rest.RestServiceAdapter;
import oracle.adfmf.framework.api.Model;
import oracle.adfmf.json.JSONArray;
import oracle.adfmf.json.JSONException;
import oracle.adfmf.json.JSONObject;

public class CallService {
    public CallService() {
        super();
    }
    List<Employee> lstEmp=new ArrayList<Employee>();
    private Employee emp;
    public void setLstEmp(List<Employee> lstEmp) {
        this.lstEmp = lstEmp;
    }

    public List<Employee> getLstEmp() {
        return lstEmp;
    }
    public void getEmployees() {
           RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter(); 
           restServiceAdapter.clearRequestProperties();  
           restServiceAdapter.setConnectionName("restConn");  
           restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);    
           restServiceAdapter.setRetryLimit(0);
           restServiceAdapter.setRequestURI("/emp");
           try {
               String response = restServiceAdapter.send(""); 
               JSONObject res = null;  
               res = new JSONObject(response);     
               JSONArray lstOfEmp=res.getJSONArray("lstEmp");
               
               for(int i=0;i<lstOfEmp.length();i++){
                  JSONObject obj=lstOfEmp.getJSONObject(i);
                  System.out.println("Emp name "+obj.getString("empName"));
                   Employee e=new Employee();
                   e.setEmpName(obj.getString("empName"));
                   lstEmp.add(e);
               }
           } catch (JSONException jsone) {
               // TODO: Add catch code
               jsone.printStackTrace();
           } catch (Exception e) {
               // TODO: Add catch code
               e.printStackTrace();
           }
       }
}

Step 5:create Data Control of CallService Class;


 Step 6: create feature in maf-feature.xml with type MAF Task Flow and create Task Flow.
create ViewActivity call named RestServiceCall and Create .amx Page

Then select getEmployees() method from data control drop in task flow:


           

In PageDef of method View add accessor iterator in executables

Step 6: drag and drop DC in Page with empName view Attribute in List View


Step 7: Deploy project on emulator

Output:


1 comment:

  1. How to set timeout to RestServiceAdapter in Oracle MAF?

    ReplyDelete