Skip to content

Service Initialization

DataService requires an implementation of the DataServiceProvider interface as a parameter to its constructor. This section uses an instance of OnlineODataProvider to interact with the service in an online fashion which sends requests directly to the service provider. Offline usage requires the OfflineODataProvider.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Create an instance of OnlineODataProvider
OnlineODataProvider provider = new OnlineODataProvider("EventService", 
    SERVICE_URL, new OkHttpClient.Builder().build(), true);
// Note: HttpHandler can be set via 
// provider.getNetworkOptions().setHttpHandler(handler) method as well

// Get an instance of DataService 
DataService dataService = new DataService(provider);

//Service and Provider level configuration operations

// Disable data service version check
// Avoid error should OData provider returns version below V2.0
provider.getServiceOptions().setCheckVersion(false);

// Configuration
// Set odata trace options
provider.setTraceRequests(true);
provider.setTraceWithData(true);
provider.setPrettyTracing(true);

// Set Atom as the data format
// provider.getServiceOptions().setDataFormat(DataFormat.ATOM);
// Set Json as the data format
provider.getServiceOptions().setDataFormat(DataFormat.JSON);

// If service does not support batch, turn off batch requests
// provider.getServiceOptions().setSupportsBatch(false);

// Allow HTTP verb tunneling over OData batch requests
provider.getNetworkOptions().setAllowTunneling(true);
// Disable compression
//provider.getNetworkOptions().setCompressResponses(false);
// Disable streamed downloads
//provider.getNetworkOptions().setStreamDownloads(false);
// Disable streamed uploads
//provider.getNetworkOptions().setStreamUploads(false);

// Ping server to check connection
dataService.pingServer();

// Get provider from service reference
provider = (OnlineODataProvider) dataService.getProvider();
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Create an instance of OnlineODataProvider
val provider = OnlineODataProvider("EventService", 
    SERVICE_URL, OkHttpClient.Builder().build(), true)
// Note: HttpHandler can also be set via 
// provider.networkOptions.httpHandler = handler

// Get an instance of DataService 
val dataService = DataService(provider)

//Service and Provider level configuration operations

// Disable data service version check
// Avoid error should OData provider returns version below V2.0
provider.serviceOptions.checkVersion = false

// Configuration
// Set odata trace options
provider.traceRequests = true
provider.traceWithData = true
provider.prettyTracing = true

// Set Atom as the data format
// provider.serviceOptions.dataFormat = DataFormat.ATOM
// Set Json as the data format
provider.serviceOptions.dataFormat = DataFormat.JSON

// If service does not support batch, turn off batch requests
// provider.serviceOptions.supportsBatch = false

// Allow HTTP verb tunneling over OData batch requests
provider.networkOptions.allowTunneling = true
// Disable compression
//provider.networkOptions.compressResponses = false
// Disable streamed downloads
//provider.networkOptions.streamDownloads = false
// Disable streamed uploads
//provider.networkOptions.streamUploads = false

// Ping server to check connection
dataService.pingServer();

// Get provider from service reference
provider = dataService.provider as OnlineODataProvider

For proxy classes, use the generated service class, which is a subclass of DataService.

Note

The name of the generated service class is specified through an argument during proxy class generation. If none is provided, it defaults to the name of the entity container in the service metadata document.

1
EventService eventService = new EventService(provider);
1
val eventService = EventService(provider)