Java API for utopian.io REST service, covers all aspects of official javascript ibrary utopian-api. Uses latest java version and http library and will work on both desktop and android platform.
utopian.java API uses features of java 8 such as lambda expressions, API is designed in such a way that it will also work on newer and older android devices. I will cover tutorial on how to setup and use this API on android.
Its best HTTP and HTTP/2 library available for both desktop and android platform. Used in millions of android and java desktop apps. Github repo stats tells how much popular it is right now.
Another reason to choose this api is i want utopian.java api to run on android platform as well.
For parsing JSON, utopian.java uses another popular JSON library from google. This library works on both desktop and android platform.
Its simple to create utopian service object
UtopianService service = new DefaultUtopianService();
service.moderators().get(); // Returns all moderators as JsonObject
service.sponsors().get(); // Returns all sponsors as JsonObject
service.stats().get(); // Returns all stats as JsonObject
service.moderator(userName).get(); // Returns moderator as JsonObject with given username otherwise empty JsonObject
service.sponsor(userName).get();// Returns sponsor as JsonObject with given username otherwise empty JsonObject
service.posts(options).get(); // Returns posts as JsonObject with given filter options
service.topProjects(options).get(); // Returns top projects as JsonArray with given filter options
service.totalPostsCount().get(); // Returns total posts count as Integer
service.post(userName, permLink).get(); // Returns post as JsonObject with given params
service.postURL(postId).get(); // Returns post url as String
service.postByAuthor(userName, options).get(); // Returns list of posts as JsonObject with given author name and filter options
service.postsByGithubProject(repoName, options).get(); // Returns list of posts as JsonObject links with specified github repository
UtopianService service = new DefaultUtopianService();
JsonObject moderatorsJson = service.moderators().get();
System.out.println("Total moderators: " +moderatorsJson.get("total"));
JsonArray results = moderatorsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("account").getAsString());
}
JsonObject sponsorsJson = service.sponsors().get();
System.out.println("Total sponsors: " +sponsorsJson.get("total"));
JsonArray results = sponsorsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("account").getAsString());
}
JsonObject statsJson = service.stats().get();
JsonObject stats = statsJson.get("stats").getAsJsonObject();
System.out.println(stats);
JsonObject moderatorJson = service.moderator("ruah").get();
if(moderatorJson.size() != 0)
System.out.println(moderatorJson.get("account").getAsString());
JsonObject sponsorJson = service.sponsor("freedom").get();
if(sponsorJson.size() != 0)
System.out.println(sponsorJson.get("account").getAsString());
JsonObject postsJson = service.posts(new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("title").getAsString());
}
Map<String, Object> filterOptions = new HashMap<>();
filterOptions.put("sortBy", "created");
filterOptions.put("type", "tutorials");
filterOptions.put("limit", 5);
JsonObject postsJson = service.posts(filterOptions).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("title").getAsString());
}
JsonObject postJson =
service.post("kabooom", "building-and-using-multiple-android-shared-libraries").get();
System.out.println(postJson.get("moderator").getAsString());
int count = service.totalPostsCount().get();
System.out.println("Total posts count: "+ count);
String url = service.postURL("38068955").get();
System.out.println("URL of post: " + url);
JsonObject postsJson = service.postByAuthor("kabooom", new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("title").getAsString());
}
JsonObject postsJson =
service.postsByGithubProject("java-native-access/jna", new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("title").getAsString());
}
JsonArray results = service.topProjects(new HashMap<>()).get();
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("_id").getAsString());
}
Contributions are always welcome, contribution to this project is simple create fork add new features or bug fixes and send a pull request.
Maven:
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.faob</groupId>
<artifactId>utopian-java</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
Gradle:
repositories {
jcenter()
}
dependencies {
compile 'io.faob:utopian-java:0.1.0'
}