API Kits can use our web service or resolve detections locally depending on your configuration.
The Java API Kit is released via JCenter. You can include it in your project using your preferred build tool, such as Gradle, Maven, SBT...
Example configuration for Gradle:
repositories {
jcenter()
}
dependencies {
compile 'com.handsetdetection:detection-apikit-java:4.2.1'
}
For detailed instructions, direct downloads and other information visit the Java API Kit page on Bintray.
API Kit configuration files can be downloaded directly from Handset Detection.
- Login to your dashboard
- Click 'Add a Site'
- Configure your new site
- Grab the config file variables for your API Kit (from the site settings)
- Place the variables into the
hdconfig.properties
file
import com.handsetdetection.HD4;
import com.handsetdetection.data.*;
// Using the default config file: hdconfig.properties in the current working directory
HD4 hd = new HD4();
// OR using a custom config file
HD4 hd = new HD4("/tmp/myCustomConfigFile.properties");
if (hd.deviceVendors()) {
Reply data = hd.getReply();
List<String> vendors = data.getVendor();
} else {
System.out.println(hd.getError());
}
if (hd.deviceModels("Nokia")) {
Reply data = hd.getReply();
List<String> models = data.getModel();
} else {
System.out.println(hd.getError());
}
if (hd.deviceView("Nokia","N95")) {
Reply data = hd.getReply();
HdSpecs deviceSpecs = data.getDevice();
} else {
System.out.println(hd.getError());
}
if (hd.deviceWhatHas("network","CDMA")) {
Reply data = hd.getReply();
List<DeviceSummary> devices = data.getDevices();
} else {
System.out.println(hd.getError());
}
hd.setDetectVar("user-agent","Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95-3/20.2.011 Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413");
hd.setDetectVar("x-wap-profile","http://nds1.nds.nokia.com/uaprof/NN95-1r100.xml");
if (hd.deviceDetect()) {
Reply data = hd.getReply();
HdSpecs deviceSpecs = data.getDevice();
} else {
System.out.println(hd.getError());
}
// Increase the default timeout before downloading the archive
hd.setTimeout(500);
if (hd.deviceFetchArchive()) {
byte[] data = hd.getRawReply();
System.out.println("Downloaded " + data.length + " bytes");
} else {
System.out.println(hd.getError());
System.out.println(Arrays.toString(hd.getRawReply()));
}
hd.setTimeout(500);
if (hd.communityFetchArchive()) {
byte[] data = hd.getRawReply();
System.out.println("Downloaded " + data.length + " bytes");
} else {
System.out.println(hd.getError());
System.out.println(Arrays.toString(hd.getRawReply()));
}
This version includes several caching options:
- In-memory (Guava Cache)
- Filesystem
- Memcached
- Redis
Include the following cache configuration in your config file.
cache.memory.maximumSize = 10000000
or simply,
cache.memory = true
Include this cache configuration in your config file:
cache.file.directory = /tmp
or simply,
cache.file = true
Include the following cache configuration in your config file.
cache.memcached.servers = server1.example.com:11211 server2.example.com:11211
or simply,
cache.memcached = true
We also have Redis as a caching option. Use a caching config as follows:
cache.redis.uri = redis://localhost/
or simply,
cache.redis = true
cache.none = true
Additional examples can be found in the Examples.java
file.
After signing up with our service you'll be on a free usage tier which entitles you to 20,000 Cloud detections (web service) per month, and access to our Community Edition for Ultimate (stand alone) detection. The archive for stand alone detection can be downloaded manually however its easiest to configure the API kit with your credentials and let the API kit do the heavy lifting for you. See examples above for how to do this.
Instructions for manually installing the archive are available at v4 API Ultimate Community Edition, Getting Started.
Unit tests use JUnit and can be found in the src/test
directory.
See the v4 API Documentation.
See the Handset Detection GitHub Repo.
Let us know if you have any hassles (hello@handsetdetection.com).