2013-02-20 67 views
0

这里的理念是:有什么需要做一个Java程序搜索谷歌?

  1. 程序(在Java中)将输入
  2. 程序接受输入,并使用它的地方在网上得到的结果
  3. 它需要它的在线得到的结果和在程序界面上显示它

有点像您如何通过Google桌面应用程序搜索而不是浏览器?

我只是需要在这方面向正确的方向普遍推动。 (也许我应该寻找一个特定的方法)我对Java API不是很熟悉。

+0

你能约你想沟通一下API更加清晰提取文本的例子用?这个问题没有一般的答案。 – duskwuff 2013-02-20 06:52:32

+0

我想我需要有一些动作监听器与输入并定义一些动作,这将使程序接受输入并使用它从网站检索数据。 – Maydayfluffy 2013-02-20 06:56:41

+0

从什么网站?请详细说明。 – duskwuff 2013-02-20 06:58:46

回答

1

您可以使用Java的标准HttpURLConnection类搜索的内容。然后,解析所有需要的响应是Apache tika,它用于从HTML页面提取文本。

下面是使用URL连接的一个简单的例子:

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 
import java.net.URLEncoder; 

public class SimpleHTTPRequest { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     HttpURLConnection connection = null; 
     DataOutputStream wr = null; 
     BufferedReader rd = null; 
     StringBuilder sb = null; 
     String line = null; 

     URL serverAddress = null; 

     try { 
      serverAddress = new URL("http://www.google.com/search?q=test"); 
      //set up out communications stuff 
      connection = null; 

      //Set up the initial connection 
      connection = (HttpURLConnection)serverAddress.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setUseCaches(false); 
      connection.setRequestProperty ("Content-type","text/xml"); 
      connection.setAllowUserInteraction(false); 
      String strData = URLEncoder.encode("test","UTF-8"); 
      connection.setRequestProperty ("Content-length", "" + strData.length()); 
      connection.setReadTimeout(10000); 
      connection.connect(); 

      //get the output stream writer and write the output to the server 
      //not needed in this example 
      wr = new DataOutputStream(connection.getOutputStream()); 
      wr.writeBytes("q="+strData); 
      wr.flush(); 

      //read the result from the server 
      rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      sb = new StringBuilder(); 

      while ((line = rd.readLine()) != null) 
      { 
       sb.append(line + '\n'); 
      } 

      System.out.println(sb.toString()); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      //close the connection, set all objects to null 
      connection.disconnect(); 
      rd = null; 
      sb = null; 
      wr = null; 
      connection = null; 
     } 
    } 
} 

而且here你找到使用Apache蒂卡

0

你必须使用URL类来连接网页。

例如

 url1 = new URL(url); 
     InputStream input=url1.openStream(); 
     BufferedInputStream bis=new BufferedInputStream(input); 
     dis=new DataInputStream(bis); 
     // byte[] buffer=new byte[1000]; 
     String data=""; 
     while(dis.available()!=0) 
     { 
      data+=dis.readLine(); 
     } 

     jobj=new JSONObject(data);