2013-02-20 72 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);