0

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.

  1. 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.
  2. 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;
    }
}
Community
  • 1
  • 1
johnmalc
  • 3
  • 4
  • What do you mean by "if user's input could be found or not"? Do you mean that the term that they typed in is present somewhere on the page http://www.example.com? Or do you mean that http://www.example.com/search?text=[user-input] actually returns some kind of positive result? Is there a key phrase that will be returned upon success? – davidfmatheson Aug 17 '12 at 15:24
  • I mean that example.com/search?text=[user-input] returns a possitive result (in this case it will be directly some product) – johnmalc Aug 17 '12 at 15:34
  • What are the success criteria, then? Is there some key phrase or some other indicator that the page is the positive result? Maybe it's just something other than "No results found" or you check that there are multiple rows with images in the main content `
    `?
    – davidfmatheson Aug 17 '12 at 15:39
  • Yes, maybe this. In the URL you will always get "example.com/descriptionOfProduct.k973311 {specific ID of that product, 6 numbers; could be also .kxxxxxx}. – johnmalc Aug 17 '12 at 15:41
  • So check that your `HttpResponse` has `descriptionOfProduct` in it. Is that enough? I'm assuming if it doesn't find anything there's a different URL that does *not* contain `descriptionOfProduct`. – davidfmatheson Aug 17 '12 at 15:44
  • Sadly i cant do that. If it doesn't find anything > 404 (yes, right). But there is always a completely different descriptionOfProduct. And it' absolutely random – johnmalc Aug 17 '12 at 15:47
  • Please edit your question to more clearly state what data you're looking for, then. Provide some example returns, URL and HTML, that come back. Then people can help you to parse the result. – davidfmatheson Aug 17 '12 at 16:39

1 Answers1

0

Once you have the remote file you could always do something like this Java Grep Library

Basically, you need to get the remote file and search through the file for a string that matches your user input.

Community
  • 1
  • 1
csilk
  • 188
  • 1
  • 15