Thursday, September 11, 2014

Query the Internet Chunk Norris Database using Java and Gson

This tutorial will show you how to use Java and the Google Gson library to query the Internet Chuck Norris Database and return a quote you can display in your application.


Step 1: Download Google Gson here: https://code.google.com/p/google-gson/

Step 2: Familiarize yourself with the icndb rest api: http://www.icndb.com

Step 3: Create a new Java project in your favorite IDE or editor and add the Gson jar library to your application and class path.

Step 4: Create a new class called Value.java:

package com.broadlyapplicable.icndb;

public class Value {

private int id;
private String joke;

public Value() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getJoke() {
return joke;
}

public void setJoke(String joke) {
this.joke = joke;
}

}


Step 5: Create a new class called Quote.java:

package com.broadlyapplicable.icndb;

public class Quote {

private String type;
private Value value;

public Quote() {
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Value getValue() {
return value;
}

public void setValue(Value value) {
this.value = value;
}

}

Step 6: Create a new class called RestUtil.java which will return a Quote object you can then use inside your application:


package com.broadlyapplicable.icndb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.Gson;

public class RestUtil {

private static final String URLPATH = "http://api.icndb.com/jokes/random";
private static final String METHOD = "GET";
private static final String CONTENT_TYPE = "application/json";

public RestUtil() {
}

public Quote getQuote(String types) {
String jsonFromRestCall = getJson(types);
Gson gson = new Gson();
Quote quote = null;
if (jsonFromRestCall != null && !jsonFromRestCall.isEmpty()) {
quote = gson.fromJson(jsonFromRestCall, Quote.class);
System.out.println(jsonFromRestCall);
}
return quote;
}

private String getJson(String types) {
String urlPath = URLPATH;
if (types != null && !types.isEmpty()) {
urlPath = urlPath + "?limitTo=" + types;
}

StringBuilder resp = new StringBuilder();
BufferedReader bufferedReader = null;
try {

HttpURLConnection conn = getConnection(urlPath);
bufferedReader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lne = null;
while ((lne = bufferedReader.readLine()) != null) {
resp.append(lne);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return resp.toString();
}

private HttpURLConnection getConnection(String urlPath) throws IOException {
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(METHOD);
conn.addRequestProperty("Context-Type", CONTENT_TYPE);
conn.setDoOutput(true);
return conn;
}
}


Step 7: Create a new class called Test.java and call the new RestUtil getQuote method passing in the types of quotes you wish to be queried after reviewing the rest api from the link above.

package com.broadlyapplicable.icndb;

public class Test {

public static void main(String[] args) {
String types = "nerdy";
RestUtil restUtil = new RestUtil();
Quote quote = restUtil.getQuote(types);
System.out.println(quote.getValue().getJoke());
}
}


Step 8: Run the Test class and you should see output similar to below:
{ "type": "success", "value": { "id": 529, "joke": "Chuck Norris doesn't use Oracle, he is the Oracle.", "categories": ["nerdy"] } }
Chuck Norris doesn't use Oracle, he is the Oracle.


I hope you enjoyed this tutorial, if you have questions or comments please leave them below, happy coding!

No comments:

Post a Comment