I am a beginner with Java and currently i am developing a SWING JAVA app.
What does this app. do.
If an user tipes some numbers (let' say) [input], then the app. connects to the specific website and searches in there (url : http://www.example.com/search?text=[user's input]). The output (i want so) will be if user's input could be found or not (with help of boolean true/false and url connection). Just this.
I already read A java program to search in a certain website, but i dont want to use crawler. Seems to hard for me. Also, i think, i dont need to parse a website, because if the user's input exists it will return what the user is looking for. So programs like jsoup are not needed.
My Question is this: What meathod should i use. How to solve this problem? Basically, how to put user's input into the website URL and then click with swing button for search on that website
EDIT: so a few points.
- The user input should return possitive result , such as this e.g. "example.com/descriptionOfProduct.k973311" where 97311 is a specific ID of that product. It has alsways 6 numbers. And the letter "k." is there always too.
- If the user input doesnt exists then 404. If it does then see above. AND the descriptionOfProduct is always a completely different. And it' absolutely random.
I use httpclient apache.
About that specific website : no api for search and it's e-commerce (eshop).
Here is my code, which is not finished because of this search functionality.
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);
and Here is the swing app
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;
}
}