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.
/* 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);
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); }
/*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);
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.
public BatchRequest batch; batch = new BatchRequest(<Service doc URL>/$batch);
public BaseRequest get1 = new BaseRequest(); get1.setRequestMethod(BaseRequest.REQUEST_METHOD_GET); get1.setRequestUrl("<collection name>"); batch.addRetrieveRequestToBatch(get1);
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);
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);