Daily Quote Webservice API Example in Java
In this example, I have written simple program about Daily Quote Webservice API Example in Java. The program has been tested and output shared in the same post.
Project Structure
Response.java
package com.dineshkrish; public class Response { private Success success; private Contents contents; public Success getSuccess() { return success; } public void setSuccess(Success success) { this.success = success; } public Contents getContents() { return contents; } public void setContents(Contents contents) { this.contents = contents; } }
Success.java
package com.dineshkrish; public class Success { private int total; public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } }
Contents.java
package com.dineshkrish; import java.util.ArrayList; public class Contents { private ArrayList<Quote> quotes; public ArrayList<Quote> getQuotes() { return quotes; } public void setQuotes(ArrayList<Quote> quotes) { this.quotes = quotes; } }
Quote.java
package com.dineshkrish; public class Quote { private String quote; private String length; private String author; private String[] tags; private String category; private String date; private String title; private String background; private String id; public String getQuote() { return quote; } public void setQuote(String quote) { this.quote = quote; } public String getLength() { return length; } public void setLength(String length) { this.length = length; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String[] getTags() { return tags; } public void setTags(String[] tags) { this.tags = tags; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBackground() { return background; } public void setBackground(String background) { this.background = background; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
GetTodayQuote.java
package com.dineshkrish; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.google.gson.Gson; /** * * @author Dinesh Krishnan * */ public class GetTodayQuote { public static void main(String[] args) { // Service provider final String SERVICE_URL = "http://quotes.rest/qod.json"; // defining Gson object Gson gson = new Gson(); try { URL url = new URL(SERVICE_URL); System.out.println("1. Calling Service...."); // opening the connection HttpURLConnection connection = (HttpURLConnection)url.openConnection(); String response = ""; // getting the InputStream from response InputStream inputStream = connection.getInputStream(); // building string from json response int data = inputStream.read(); while(data != -1) { response = response + (char)data; data = inputStream.read(); } inputStream.close(); System.out.println("2. Rendering the response...."); // converting Json to object Response responseObj = gson.fromJson(response, Response.class); System.out.println("3. Printing the result..."); System.out.println("------------------------------"); Quote quote = responseObj.getContents().getQuotes().get(0); // printing the result System.out.println("Today ("+quote.getDate()+") Quote : "+quote.getQuote()); System.out.println("Author : "+quote.getAuthor()); } catch (MalformedURLException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
JSON Response
{ "success": { "total": 1 }, "contents": { "quotes": [ { "quote": "Own 100 percent of your focus. The most challenging of endurance drills will bring you to a level of optimal mental and physical performance.", "length": "141", "author": "Lorii Myers", "tags": [ "endurance", "focus", "in-the-zone", "inspire", "optimum" ], "category": "inspire", "date": "2017-01-19", "title": "Inspiring Quote of the day", "background": "https://theysaidso.com/img/bgs/man_on_the_mountain.jpg", "id": "ejoiFh5LCmr_bp6PiNVdtweF" } ] } }
Output
1. Calling Service….
2. Rendering the response….
3. Printing the result…
——————————
[su_highlight]Today (2017-01-19) Quote : Own 100 percent of your focus. The most challenging of endurance drills will bring you to a level of optimal mental and physical performance.
Author : Lorii Myers[/su_highlight]
References
1. https://theysaidso.com/api/
2. Quote For a Day
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet