0

I want to use the google-suggest-api in my Java application.

Can i call http://google.com/complete/search?q=Test&output=toolbar via a Java application?

Please give me an example.

Updated.

Below code is work in my java application but when I use in java servlet (apache tomcat 6.0 is my web server) it return result without suggest content "< ?xml version="1.0"?>< toplevel/>" why?

private void getSuggestWord(String keyword){

    try {
        String urlName = "http://google.com/complete/search?q=" + keyword + "&output=toolbar";
        System.out.println(urlName);
        URL url = new URL(urlName);
        URLConnection conn = url.openConnection();
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723                       Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = in.readLine();
        in.close();
        System.out.println(line);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

1 Answers1

0

I believe you can.
Start reading about the Apache HTTP client, and see the examples there.
In general what you need to do is - A. Create an HTTP connection
B. Post your Requerst
C. Read the result
D. Parse the result

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27