Java Socket Program to Find Meaning of Word Using Oxford Dictionary REST API Full Example For Beginners

  • Post author:
  • Post category:Java
  • Post comments:0 Comments

 

 

main.java

 

 

import  javax.net.ssl.HttpsURLConnection;
import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.net.URL;
 public class Oxford {
      public static void main(String[] args) {
       final String language = "en-gb";
       final String word = "Ace";
       final String fields = "pronunciations";
       final String strictMatch = "false";
       final String word_id = word.toLowerCase();
       final String restUrl = "https://od-api.oxforddictionaries.com:443/api/v2/entries/" + language + "/" + word_id + "?" + "fields=" + fields + "&strictMatch=" + strictMatch;
       //TODO: replace with your own app id and app key
      final String app_id = "";
       final String app_key = ""; 
       try {
         URL url = new URL(restUrl);
         HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
         urlConnection.setRequestProperty("Accept", "application/json");
         urlConnection.setRequestProperty("app_id", app_id);
         urlConnection.setRequestProperty("app_key", app_key);

         // read the output from the server
         BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
         StringBuilder stringBuilder = new StringBuilder();
         String line = null;
         while ((line = reader.readLine()) != null) {
             stringBuilder.append(line + "\n");
        }
         System.out.println(stringBuilder.toString());
      } catch (IOException e) {
         e.printStackTrace();
      }
    }
}

Leave a Reply