Results 1 to 3 of 3
  1. #1
    avizana is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default get url returned by search

    I'm sending a query for Wikipedia site as below:

    Java - Wikipedia, the free encyclopedia

    The webpage that I'm getting is: Java - Wikipedia, the free encyclopedia

    How can I get the URL that the webpage is returning?

    Java Code:
    URL u = new URL("http://en.wikipedia.org/wiki/?search=Java");

  2. #2
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Just found this: API:Main page - MediaWiki

    It is the mediawiki API (Wikipedia is a part of it). Chances are, part of the API is a search function. EDIT: This is what I found. Use this:
    Java Code:
    http://en.wikipedia.org/w/api.php?action=opensearch&search=Java
    This returns possible pages. Pick the correct one, replace spaces with underscores, and go to the wikipedia page.

    EDIT EDIT: Here is a working method and example. It takes the query, and which result number to return (starting from 1, 0 will just give you your query). If it is out of bounds, it return an empty string.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package urltest;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author phani
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                String query = "Java";
    
                System.out.println(searchWikipediaURL(query,1));
            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        }
    
        public static String searchWikipediaURL(String query, int result) throws Exception {
            try{
            URL url = new URL("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + query);
            InputStream is = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String results = br.readLine();
            return("http://en.wikipedia.org/wiki/"+results.substring(nthOccurrence(results, '"', result*2)+1, nthOccurrence(results, '"', (result*2)+1)));
            }
            catch(Exception e){
                return "";
            }
        }
    
        public static int nthOccurrence(String str, char c, int n) {
            int pos = str.indexOf(c, 0);
            while (n-- > 0 && pos != -1) {
                pos = str.indexOf(c, pos + 1);
            }
            return pos;
        }
    }
    EDITx3: Not sure if u need to replace the spaces. It works w/ spaces in a browser, not sure about Java.
    Last edited by rp181; 03-13-2011 at 11:11 PM.

  3. #3
    avizana is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. 2 Items returned on itemStateChanged() Test???
    By AcousticBruce in forum New To Java
    Replies: 1
    Last Post: 01-08-2011, 04:10 PM
  2. SplashScreen.getSplashScreen() returned null
    By kmm1977 in forum AWT / Swing
    Replies: 4
    Last Post: 06-21-2010, 11:04 AM
  3. SplashScreen.getSplashScreen() returned null
    By ibrahimyoussof in forum AWT / Swing
    Replies: 2
    Last Post: 04-17-2010, 02:51 PM
  4. Cant we have two values returned from a method in a class
    By jaiminparikh in forum Advanced Java
    Replies: 7
    Last Post: 03-21-2009, 03:20 AM
  5. Returned type changes if class is extended. How?
    By Gelembjuk in forum New To Java
    Replies: 10
    Last Post: 10-31-2008, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •