[utopian.java v0.3.1] - Support for api key, mapping new json fields to java
Overview
Recently utopian-io added API usage limit this release added support for api key, java types has been updated to reflect new changes made to json and plus some other code improvements.
For more information on previous versions
Requirements
- Java 8
utopian.java API uses features of java 8 such as lambda expressions, Subset of Java 8 features has been used in this library so that it can run on older android devices and latest ones as well as desktop Java.
Code Commits
Recently utopian-io added API usage limit to the system here is the link of the post
In order to use utopian-java library now you will require API key which you can request from here. This commit added support for API key feature added to system. Once you get your API key then you just have to do these steps
System.setProperty("API_KEY", "your-key");
System.setProperty("API_KEY_ID", "your-key-id");
UtopianService service = new DefaultUtopianService();
In this commit some redundant code has been removed to make code simpler.
utopian-io added some new fields in Post JSON and since utopian-java library has Java type mapping for JSON, so in this commit Java Post class has been updated to reflect new changes.
In Post JSON, inside json_metadata there is a field questions sometimes it has type JSON object and sometimes it has type JSON array so it was creating issue. In this commit i added custom json deserializer to check the type and parse according to type.
Post post = service
.post("kabooom", "utopian-java-v0-1-0-utopian-api-for-java-and-android")
.asJavaType()
.get();
//if questions type is JSON Object
System.out.println(post.getJsonMetadata().getQuestionsObject());
//if questions type is JSON Array
System.out.println(post.getJsonMetadata().getQuestionsArray());
Using utopian-java in android project
In this simple tutorial we will use utopian-java in android project. We will target subset of java 8 features that can run on all android API levels.
Requirements
- Android Studio 3.0 or higher
1. Creating Android Studio project
Create new android project and change Application name you can also change Company domain and Package name according to requirements and click next
Select minimum SDK version and click next
Select Empty Activity and click next
Change Activity Name and Layout Name according to requirements and click Finish to create project
Now we will add support for Java 8 click on File menu and then click on Project Structure and then select Source Compatibility and Target Compatibility to 1.8
Now we will add utopian-java library to our project for this open build.gradle and add this line
dependencies {
implementation 'io.faob:utopian-java:0.3.1'
...
...
}
Since our app will use internet so lets add Internet permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET"/>
2. Using Java 8 lambda expressions
In our simple app we will just get 50 latest posts and show in text view we will use getAsync() to get posts in background thread and update textview in lambda expression. Here is the complete code and output
public class MainActivity extends AppCompatActivity {
private String titles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.setProperty("API_KEY", "your-key");
System.setProperty("API_KEY_ID", "your-key-id");
UtopianService service = new DefaultUtopianService();
service.posts(new HashMap<>())
.asJavaListType() // get posts as java type
.getAsync(posts -> { // success handler with List<Post>
for (Post post : posts) {
titles += post.getTitle() + "\n\n";
}
//its background thread so update textview on UI thread
runOnUiThread(() -> {
TextView textView = findViewById(R.id.textView);
textView.setText(titles);
});
}, e -> { // error handler to handle exceptions
e.printStackTrace();
});
}
}
3. Using Java 8 method references
If the code is big in lambda expression then code become very ugly and difficult to maintain to solve this problem we can use method reference. Here is complete code and output
public class MainActivity extends AppCompatActivity {
private String titles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.setProperty("API_KEY", "your-key");
System.setProperty("API_KEY_ID", "your-key-id");
UtopianService service = new DefaultUtopianService();
service.posts(new HashMap<>())
.asJavaListType() // get posts as java type
.getAsync(
//handle success in this method reference
this::handleResponse,
//handle exception in this method reference
this::handleError);
}
//success handler
private void handleResponse(List<Post> posts) {
for (Post post : posts) {
titles += post.getTitle() + "\n\n";
}
//its background thread so update textview on UI thread
runOnUiThread(() -> {
TextView textView = findViewById(R.id.textView);
textView.setText(titles);
});
}
//exception handler
private void handleError(Exception e) {
e.printStackTrace();
}
}
Contribution
Contributions are always welcome, contribution to this project is simple create fork add new features or bug fixes and send a pull request.
Download
Maven:
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.faob</groupId>
<artifactId>utopian-java</artifactId>
<version>0.3.1</version>
</dependency>
</dependencies>
Gradle:
repositories {
jcenter()
}
dependencies {
compile 'io.faob:utopian-java:0.3.1'
}
Github
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @kabooom
We're already looking forward to your next contribution!
Decentralised Rewards
Share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.
Utopian Witness!
Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.
Want to chat? Join us on Discord https://discord.me/utopian-io
I will upvote and resteem your last blog post to my 36,000+ followers for free if you reply to this comment. Follow @a-0-0
Thank you for the contribution. It has been reviewed.
Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.
[utopian-moderator]
thanks, i updated the post and included comments
Congratulations @kabooom! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the total payout received
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP