Created
March 19, 2017 11:10
-
-
Save ahmedengu/d06a0844490fa49cbbf661212a33e558 to your computer and use it in GitHub Desktop.
RMI oxford dictionary api, JSON parsing using json-simple: https://code.google.com/archive/p/json-simple/downloads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.registry.LocateRegistry; | |
import java.util.Scanner; | |
/** | |
* Created by ahmedengu. | |
*/ | |
public class Client { | |
private static OxfordInterface oxford; | |
public static void main(String[] args) { | |
try { | |
oxford = (OxfordInterface) LocateRegistry.getRegistry("localhost", 1234).lookup("//localhost/dictionary"); | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Write a word[-1 to exit]:"); | |
String read = scanner.nextLine(); | |
while (!read.contains("-1")) { | |
System.out.println(oxford.dictionary(read)); | |
System.out.println("Write a word[-1 to exit]:"); | |
read = scanner.nextLine(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.rmi.RemoteException; | |
import java.rmi.server.UnicastRemoteObject; | |
/** | |
* Created by ahmedengu. | |
*/ | |
public class Oxford extends UnicastRemoteObject implements OxfordInterface { | |
protected Oxford() throws RemoteException { | |
} | |
public String dictionary(String word) { | |
JSONParser parser = new JSONParser(); | |
String ret = "404"; | |
try { | |
final String result = getRequest(buildURL(word)); | |
final Object parse = parser.parse(result); | |
ret = (String) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) ((JSONArray) ((JSONObject) parse).get("results")).get(0)).get("lexicalEntries")).get(0)).get("entries")).get(0)).get("senses")).get(0)).get("definitions")).get(0); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
return ret; | |
} | |
private String buildURL(final String word) { | |
final String language = "en"; | |
final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required | |
return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id; | |
} | |
private String getRequest(String link) { | |
final String app_id = "2f0d7a3a"; | |
final String app_key = "5215f3131a55b7cdabb65c4193c63d49"; | |
try { | |
URL url = new URL(link); | |
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"); | |
} | |
return stringBuilder.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return e.toString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
/** | |
* Created by ahmedengu. | |
*/ | |
public interface OxfordInterface extends Remote { | |
public String dictionary(String word) throws RemoteException; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.registry.LocateRegistry; | |
public class Server extends Thread { | |
public static void main(String args[]) { | |
try { | |
LocateRegistry.createRegistry(1234).rebind("//localhost/dictionary", new Oxford()); | |
System.out.println("Server ready"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment