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:
org.json
Add these dependencies to your project’s build file. If you’re using Maven, add the following to your pom.xml
:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
If you’re using Gradle, add the following to your build.gradle
:
dependencies {
implementation 'org.json:json:20231013'
}
Parameters:
- apiKey: Your API key from the FleetShare SDK program.
- deviceName: A unique name for the device running the SDK.
- baseDir: If set to
null
, It uses default sdk location to store the necessary files. Set absolute path if you have idea about the system path which have proper read and write access. - showLogs: (Optional) If set to
true
, the SDK print the logs to the console.
Example Implementation
Here’s an example of integrating the FleetShare SDK into a Java application:
package org.example;
import com.fleet.FleetSdk;
public class Main {
private static final Object lock = new Object();
private static final String API_KEY = "YOUR_API_KEY";
private static final String DEVICE_NAME = "ANY_RANDOM_NAME";
private static final boolean showLogs = false;
// baseDir is where all files are stored.
// If your system has different read-write system
// pass the absolute file path where this sdk can access and keep files stored.
// By default, it stores where the sdk is running.
private static final String baseDir = null;
public static void main(String[] args) throws Exception {
FleetSdk sdk = new FleetSdk(API_KEY, DEVICE_NAME, showLogs, baseDir);
boolean initializationResult = sdk.initialize();
if (!initializationResult) {
System.err.println("Failed to initialize SDK.");
return;
}
boolean isSdkStarted = sdk.startSdk();
if (!isSdkStarted) {
System.err.println("Failed to start SDK.");
return;
}
System.out.println("SDK started. Holding main thread...");
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
System.err.println("Main thread interrupted.");
Thread.currentThread().interrupt();
}
System.out.println("Main thread exiting.");
}
}
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 with the proper logs ( set showLogs to true
to get logs )