0

これがアイデアです:

  1. プログラム(Java)は入力を受け取ります
  2. プログラムは入力を受け取り、それをオンラインのどこかで使用して結果を取得します
  3. オンラインで取得した結果を取得し、プログラム インターフェイスに表示します。

ブラウザではなく、Google デスクトップ アプリケーションで検索できるようなものでしょうか。

これについては、正しい方向への一般的なプッシュが必要です。(私が探すべき特定のメソッドかもしれません) 私は Java API にあまり詳しくありません。

4

4 に答える 4

1

Java の標準 HttpURLConnection を使用してコンテンツを検索できます。次に、応答を解析するために必要なのは、HTML ページからテキストを抽出するために使用されるApache tikaだけです。

Url Connection を使用する簡単な例を次に示します。

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;
      }
  }
}

ここでは、apache tikaを使用してテキストを抽出する例を示します。

于 2013-02-20T08:19:31.670 に答える
0

Java ソケット プログラミングと Web サーバーのしくみについて学習する必要があります。これに伴い、HttpURLConnectionクラスを使用して Web サーバーへの接続を確立し、コンテンツをダウンロードできます。

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/HttpURLConnection.html

于 2013-02-20T06:53:53.057 に答える
0

オープン ソース ライブラリのApache Http コンポーネントを使用できます。これにより、作業が楽になります。

于 2013-02-20T06:56:46.017 に答える