Build a Wikipedia URL

How can I build a Wikipedia URL in java language? For example if search about "javascript" the url will be : https://en.wikipedia.org/wiki/JavaScript

Is there any API ready to use?

1 answer

  • answered 2017-11-14 23:31 user3362334

    I am not aware of any such API. However you can make your own factory method:

        public class WikipediaURLFactory {
    
        private static final String WIKIPEDIA_BASE_URL = "https://en.wikipedia.org/wiki/";
    
        public static String createWikiURLString(String search) {
            return WIKIPEDIA_BASE_URL + search;
        }
    
        public static URL createWikiURL(String search) throws MalformedURLException {
            return new URL(createWikiURLString(search));
        }
    
        public static Status accessPage (URL url) throws IOException {
            Status status = new Status();
            status.setUrl(url);
            status.setExists(true);
    
            if (getResponseCode(url) == 404) {
                status.setExists(false);
            }
    
            return status; 
        }
    
        private static int getResponseCode (URL url) throws IOException {
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
    
            return connection.getResponseCode();
        }
    }
    

    Your status class:

    private boolean exists;
        private URL url;
    
        public Status () {}
    
        public boolean isExists() {
            return exists;
        }
    
        public void setExists(boolean exists) {
            this.exists = exists;
        }
    
        public URL getUrl() {
            return url;
        }
    
        public void setUrl(URL url) {
            this.url = url;
        }
    

    And here is the main test class:

    public class Main {
    
        public static void main(String[] args) {
    
            try {
    
                // this will return true
                URL url = WikipediaURLFactory.createWikiURL("JavaScript");
                Status status = WikipediaURLFactory.accessPage(url);
                String negation = status.isExists() ? "" : "doesn't";
                System.out.println("The webpage " + url + " " + negation + " exist");
    
                // this will return false as page JafaScript doesn't exist on wiki
                url = WikipediaURLFactory.createWikiURL("JafaScript");
                status = WikipediaURLFactory.accessPage(url);
                negation = status.isExists() ? "" : "doesn't";
                System.out.println("The webpage " + url + " " + negation + " exist");
    
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    

    You may add other necessary fields in Status class (for example page content) if you need them. This is just an example.