Regular Java API Example

Hello,
I found a Java example of how to use the oxford dictionaries API. However, it looks like that it is for using Android development as it uses libraries like (android.os) and (AppCompatActivity) that are for Android application development. I want a sample of how to use it in regular Java with the needed libraries.

Thank you.

Tagged:

Comments

  • TaisFukushimaTaisFukushima Member, Administrator, Moderator admin

    Hello @mujtaba1990

    The Java example maintained by our team is in the API Documentation page.

    Please, see where to find them in the image attached. We would recommend following the examples we have in the API website, these are the examples supported by the Oxford Dictionaries API team.

    I hope it helps!

  • mujtaba1990mujtaba1990 Member

    @TaisFukushima but that's an example for Android applications. I want a normal one like the python example.

  • TaisFukushimaTaisFukushima Member, Administrator, Moderator admin
    edited January 29

    Hello @mujtaba1990 Sorry it isn't what you are looking for. I checked with my colleagues from the technology team and they have informed that the core bits of this example will work without Android in new and old Java versions. There are also many other ways to make the request using other Java libraries which may be easier or shorter. Please see this link.

    import javax.net.ssl.HttpsURLConnection;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    
    public class Test {
    
        public static void main(String[] argsString) {
            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 = "<my_app_id>";
            final String app_key = "<my_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();
            }
        }
    }
    

    I hope it helps! Let me know if you have further questions.

  • miurahrmiurahr Member

    Sorry for late relay.

    I've published an application that access OD API in plain Java as FreeSoftware.
    You can find a parser source on github and parse DTD is placed here

    Note: A license of code is GPL-3, so please be careful when you want to use it on your application.

Sign In or Register to comment.