Java Integration
This guide provides instructions for integrating the FleetShare SDK into a Java application. The SDK uses multithreading to run concurrently with your main application logic. Java is a widely-used, object-oriented programming language known for its “write once, run anywhere” capability.
Requirements
Before you begin, ensure you have the following dependencies in your project:
java.util.concurrent
java.net
java.io
java.security
org.json
com.neovisionaries.ws.client
Add these dependencies to your project’s build file. If you’re using Maven, add the following to your pom.xml
:
<dependencies>
<!-- JSON library -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<!-- WebSocket client library -->
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-websocket-client</artifactId>
<version>2.14</version>
</dependency>
</dependencies>
If you’re using Gradle, add the following to your build.gradle
:
dependencies {
implementation 'org.json:json:20210307'
implementation 'com.neovisionaries:nv-websocket-client:2.14'
}
Parameters:
- apiKey: Your API key from the FleetShare SDK program.
- silent: (Optional) If set to
true
, the SDK runs silently without outputting logs. - deviceName: (Optional) A unique name for the device running the SDK.
Example Implementation
Here’s an example of integrating the FleetShare SDK into a Java application:
package com.example;
public class Main {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY";
boolean silent = false;
String deviceName = "ANY_RANDOM_NAME";
// Create a new thread for running the SDK
Thread sdkThread = new Thread(() -> {
try {
FleetShareSDK.startFleetShareSDK(apiKey, silent, deviceName);
} catch (Exception e) {
e.printStackTrace();
}
});
// Start the SDK thread
sdkThread.start();
try {
// Your main program logic goes here
for (int i = 0; i < 10; i++) {
System.out.println("Main program is running... (" + (i + 1) + ")");
Thread.sleep(1000); // Wait for 1 second
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Best Practices
- Error Handling: Implement robust error handling to manage potential issues that may arise during SDK operation.
- User Consent: Ensure you obtain user consent before initiating the bandwidth sharing process.
- Updating the SDK: Stay updated with new SDK versions and implement mechanisms to handle SDK updates gracefully, as older versions may become deprecated.
- Resource Management: Be mindful of system resources. The SDK is designed to run efficiently, but you should monitor its impact on your application’s performance.
Ensure you follow these best practices to maintain a secure and user-friendly application.
Troubleshooting
If you encounter any issues while integrating or using the FleetShare SDK, consider the following:
- Ensure you’re using the latest version of the SDK.
- Check that all required dependencies are correctly added to your project.
- Verify that your API key is correct and active.
- If you’re experiencing connection issues, check your network connectivity.
- Review your Java version compatibility. The SDK may require a specific Java version or higher.
- Do not modify the SDK’s internal code. Doing so may cause unexpected behavior or errors.
For further assistance, please contact the FleetShare support team.