私は Java の初心者で、現在 SWING JAVA アプリを開発しています。
このアプリは何をしますか。行う。
ユーザーがいくつかの数字を入力した場合 (たとえば) [入力]、アプリ。特定の Web サイトに接続し、そこで検索します (url : http://www.example.com/search?text=[user's input])。出力(私はそうしたい)は、ユーザーの入力が見つかったかどうかです(ブール値の真/偽とURL接続の助けを借りて)。これだけ。
特定の Web サイトを検索するための Java プログラムを既に読みましたが、クローラーを使用したくありません。私には難しいようです。また、ユーザーの入力が存在する場合、ユーザーが探しているものを返すため、Web サイトを解析する必要はないと思います。したがって、jsoup のようなプログラムは必要ありません。
私の質問は次のとおりです。どの方法を使用すればよいですか。この問題を解決するには?基本的に、ユーザーの入力をウェブサイトの URL に入力し、スイング ボタンをクリックしてそのウェブサイトで検索する方法
編集: いくつかのポイント。
- ユーザー入力は、たとえば「example.com/descriptionOfProduct.k973311」のように、肯定的な結果を返す必要があります。97311 はその製品の特定の ID です。常に6つの数字があります。そして「k」の文字。もいつもあります。
- ユーザー入力が存在しない場合は 404 です。存在する場合は、上記を参照してください。そして、descriptionOfProduct は常に完全に異なります。そして、それは完全にランダムです。
httpclient Apache を使用しています。
その特定のWebサイトについて:検索用のAPIはなく、eコマース(eshop)です。
これが私のコードですが、この検索機能のために完成していません。
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
public class Connection {
/**
* @param args
* @throws URISyntaxException
*/
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws URISyntaxException {
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.example.cz");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
connection.getInputStream();
System.out.println("Connected to website");
// do something with the input stream here
// InputStream error = ((HttpURLConnection) connection).getErrorStream();
// # means special ID to understand where & what is wrong
} catch (MalformedURLException e1) {
e1.printStackTrace();
System.err.println("Something's wrong #1");
} catch (IOException e1) {
e1.printStackTrace();
System.err.println("Something's wrong #2");
} finally {
if(null != connection) { connection.disconnect(); }
}
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("www.example.com").setPath("/search")
.setParameter("text", "");
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
スイングアプリはこちら
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
/**
* @author John Malc
* @version
*
*/
public class app {
private JFrame frame;
private JTextField textField;
private JButton btnSearch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
app window = new app();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public app() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(getTextField(), BorderLayout.NORTH);
frame.getContentPane().add(getBtnSearch(), BorderLayout.SOUTH);
}
private JTextField getTextField() {
if (textField == null) {
textField = new JTextField();
textField.setColumns(10);
}
return textField;
}
private JButton getBtnSearch() {
if (btnSearch == null) {
btnSearch = new JButton("search");
}
return btnSearch;
}
}