Invoking a Request

Send an asynchronous data request to the backend through the SAP Mobile Platform Server. With the offline capability, Request library requests are queued in a persistent database, if there is no network connectivity, and invoked when the network becomes available. You can use the Request library to develop applications that have direct connectivity to Gateway or to register users on Management Cockpit.

Creating a Request

The package structure for the Request library is com.sap.mobile.lib.request.
Note: Once the listener is set on the RequestManager class, all POST, PUT, DELETE (except GET) responses are received only on this listener. For GET requests, the listener can be set on corresponding request object itself.
  1. To enable offline capability, set the response listeners using the following code, before invoking the request:
    /* Setting the listener */
    public class SampleActivity extends Activity implements INetListener {
    @Override
    public void onError(IRequest arg0, IResponse arg1,
    IRequestStateElement arg2) {}
    
    @Override
    public void onSuccess(IRequest arg0, IResponse arg1) {}
    }
    .
    .
    // setting the listener to current class
    RequestManager reqMan = new RequestManager(logger, pref, param, 1);
    reqMan.setListener(listener); 
    
    
    • Set the listener only once per application.
    • For all GET requests, the listener is always set on the request object.
    • If offline capability is disabled, then for POST, PUT, and DELETE requests also, the listener is set on corresponding request.
  2. For all GET requests the listener is always set on request object. If offline capability is not enabled, then for POST, PUT, and DELETE requests also, listener is set on corresponding request.

    Proceed with the following code to set response listener on request object:

    public class SampleActivity extends Activity implements INetListener {
    @Override
    public void onError(IRequest arg0, IResponse arg1,
    IRequestStateElement arg2) {}
    
    @Override
    public void onSuccess(IRequest arg0, IResponse arg1) {}
    .
    .
    
    getrequest.setListener(this);
    }
    
  3. Proceed with the following code to create the request asynchronously:
    /*Enable XCSRF handling for OData requests*/
    param.enableXsrf(true);
    BaseRequest getEntry = new BaseRequest();
    /*Initialize the Requesting class with the endpoint URL after successful registration and set the headers */
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/atom+xml");
    getrequest.setHeaders(headers);
    getrequest.setRequestUrl("<Service doc URL>");
    getEntry.setRequestMethod(<Request-Method>);
    /*Set user name */
    param.setUserName(<UserName>);
    /*Set password */
    param.setUserPassword(<Password>);
    /*Set the listener to receive the response */
    getrequest.setListener(this);
    

Creating a Batch Request

Batch processing allows you to send multiple requests in a single batch request to the Gateway.  The batch request is a multipart POST request that contains one or more changesets comprising of other POST, PUT, or DELETE requests.  The batch request can also contain GET-based retrieval requests. The Gateway processes the batch request, and the responses to the various requests that make up the batch request are combined into a single response and sent back to the user as a single response.

  1. Initialize the batch request:
    public BatchRequest batch; 
    batch = new BatchRequest(<Service doc URL>/$batch);
    
  2. Add a retrieve request to the batch request:
    public BaseRequest get1 = new BaseRequest();		
    get1.setRequestMethod(BaseRequest.REQUEST_METHOD_GET); get1.setRequestUrl("<collection name>");
    batch.addRetrieveRequestToBatch(get1);
    
  3. Add request to a changeset:
    public BaseRequest post1 = new BaseRequest();  
    post1.setRequestMethod(BaseRequest.REQUEST_METHOD_POST);
    Map<String, String> headers1 = new HashMap<String, String>();
    		headers1.put("Content-Type", "application/atom+xml");
    post1.setHeaders(headers1);
    post1.setRequestUrl("TravelagencyCollection");
    
    //For single request
    IODataEntry entry = new ODataEntry(metaDataDocument);
    entry.setCollectionId(<collectionId>);
        Iterator<IODataProperty> entryPropIter = entry.getPropertiesData().iterator();
        IODataProperty entryProp;
        entry.putPropertyValue(“agencynum”, “agency001”);
        entry.putPropertyValue(“NAME”, “travelagency001”);
        String postBody = parser.buildODataEntryRequestBody(entry, collectionId, metaDataDocument, 1);
        
        post1.setData(postBody.getBytes());  
        batch = new BatchRequest(<Service doc URL>);
        batch.addRequestToChangeset(post1);
    
    Note:
    • The POST URL should be relative to the batch URL.
    • Change sets are update requests. For the change set to be successfully processed, first do a GET operation on the URL to fetch the XSRF token. Then, after handling of XSRF token is done by the SDK implicitly.

Making a Request

Initialize the RequestManager and make a request.

//To initialise the Request manager
import com.sap.mobile.lib.request.Logger;
import com.sap.mobile.lib.request.Preferences;
import com.sap.mobile.lib.request.RequestManager;
import com.sap.mobile.lib.request.ConnectivityParameters;

....

Logger logger = null;
Preferences pref = null;
ConnectivityParameters param = null;
RequestManager requestManager= null;


logger = new Logger();
//
pref = new Preferences(this.getApplicationContext(), logger);
try {
pref.setBooleanPreference(IPreferences.PERSISTENCE_SECUREMODE, false);
pref.setIntPreference(IPreferences.CONNECTIVITY_HTTPS_PORT, 443);
pref.setIntPreference(IPreferences.CONNECTIVITY_CONNTIMEOUT, 7000);
pref.setIntPreference(IPreferences.CONNECTIVITY_SCONNTIMEOUT, 7000);

} catch (PreferencesException e) {
logger.e("preferences", e.getMessage(), e); 
e.printStackTrace();
}

param = new ConnectivityParameters();	
param.setUserName("username");
param.setUserPassword("password");
param.enableXsrf(true);
requestManager = new RequestManager(logger, pref, param, 1);
//To make a request
requestManager.makeRequest(getrequest);