Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Yhlas */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ListIterator; import java.util.Scanner; import java.util.Vector; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class DefinitionObject { /*** DATA MEMBERS ***/ public String serverOutput; public String definition; public String entry; public String subject; protected Vector<String> defList = new Vector<String>(); protected ListIterator iter; int usage; int last_query_index; /*** FUNCTIONS ***/ /*** Calls for server output ***/ public void fetch_output(String word) { entry = word; System.out.print("----------------\n" + "Word to be defined:\t " + word + "\n----------------"); try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet getRequest = new HttpGet( "https://api.pearson.com:443/v2/dictionaries/entries?headword=" + word); //Here is word! getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("\nOutput from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); serverOutput = output; } httpClient.getConnectionManager().shutdown(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /*** default constructor ***/ public DefinitionObject() { serverOutput = "NULL"; definition = "NULL"; entry = "NULL"; usage = 0; } /*** Constructor given word-to-be-defined ***/ /*FINISH THIS FUNCTION */ public DefinitionObject(String word) { entry = word; boolean custom = false; fetch_output(entry); fetch_def(); usage += 1; } /*** Shows JSON-format server output ***/ public void showOutput() { System.out.println("This is from Def_Obj class ..."); System.out.println(serverOutput); } /*** Find definition of provided word ***/ public void fetch_def() { String str = serverOutput; Scanner scan = new Scanner(serverOutput); String substr = new String("definition"); String subystr = new String(":"); String quote = new String("\""); /*** finding indeces to parse first definition found ***/ last_query_index = 0; do { int defindex = str.indexOf(substr, last_query_index + 9); int colonindex = str.indexOf(subystr, defindex); int beginningofdef = str.indexOf(quote, colonindex);//sub2 int endofdef = str.indexOf(quote, beginningofdef + 1);//sub3 definition = new String(str.substring(beginningofdef + 1, endofdef)); //System.out.println("\nDefinition of word: "+"\""+definition+"\""); last_query_index = defindex; defList.add(definition); //System.out.println(defList.lastElement()); } while (!definition.equals("offset")); defList.remove("offset"); iter = defList.listIterator(); } public boolean hasSpace(String word) { for (int i = 0; i < word.length(); i++) { if (Character.isWhitespace(word.charAt(i))) return true; } return false; } }