Question

I made some research to solve my problem but sadly until now I couldn't. It's not such a big deal but I've stuck on it..

I need to make a search with some keywords in search engines such as google. I got two class here to do this:

    package com.sh.st;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class EventSearch extends SearchScreen implements ActionListener {

    public EventSearch(){

        btsearch.addActionListener(this);

    }

        public void actionPerformed(ActionEvent e){

            if(e.getSource()==btsearch){
            String query=txtsearch.getText();
            }

        }



}

and

    package com.sh.st;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class HttpRequest extends SearchScreen 
{
    URL url = new URL("google.com" + "?" + query).openConnection();
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Accept-Charset", "UTF-8"); //Possible Incompatibility
    InputStream response = connection.getInputStream();

}

So, txtsearch comes from another class named SearchScreen and I attributed the value to one string named query. I need to pass query to HttpRequest class and to do this I just extend, I'm sure it's wrong but I saw someone else doing this; and this is the first problem, how may I do this?

the second and most important I'm receiving syntax error:

enter image description here enter image description here

I didn't fully understand the meaning and utility of "connection.setRequestProperty("Accept-Charset", "UTF-8");" 'course reading I can understand that is regarding the caracters that probably will come up from my request but even though the syntax error is not clear for me

I made research in links such:

  1. How to send HTTP request in java?
  2. getting text from password field
  3. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  4. Using java.net.URLConnection to fire and handle HTTP requests

All of them have a good material but I can't fully understand everything on it and the part the I'm trying to follow is not working. Could anyone help me please?

Edit: [Topic Solved]

Was it helpful?

Solution

Try this code: (comments inlined)

// Fixed search URL; drop openConnection() at the end
URL url = new URL("http://google.com/search?q=" + query);

// Setup connection properties (this doesn't open the connection)
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");

// Actually, open the HTTP connection
connection.connect();

// Setup a reader
BufferedReader reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));

// Read line by line
String line = null;
while ((line = reader.readLine()) != null) {
     System.out.println (line);
}

// Close connection
reader.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top