SRL Archive - Help and Support

Java Web Browser Help


[XoL]

2013-05-02 22:24:57

Solved!!

Thanks Brandon and Le Jingle!

Brandon

2013-05-03 01:03:44

First lesson for all programming languages.. There can only be "ONE" main.. You on the other hand have two. One in your client class and another in your LBGui class.. is there a reason for this? Are these classes in two separate programs? That is the only time you'd have more than one main.


Second, why use a socket in java? That's leaning towards C++ if you ask me as Java/C# provide classes for reading a webpage/sending a Get request unless you plan on sending custom GET's. By the looks of your code, seems you just want to get the url source..

Since this is a browser, I would assume the following two functions would be useful to you:


public static void downloadFile(URL Address, String Path) throws IOException {
URLConnection Connection = Address.openConnection();
try (FileOutputStream OutFile = new FileOutputStream(Path); InputStream IStream = Connection.getInputStream()) {
byte[] Buffer = new byte[1024];
for (int Read = 0; (Read = IStream.read(Buffer)) != -1;) {
OutFile.write(Buffer, 0, Read);
}
}
}


//Use Connection.addRequestProperty to add a field to the protocol.

public static String getPage(URL Address) throws IOException {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
byte[] Buffer = new byte[Connection.getContentLength()];
try (DataInputStream Stream = new DataInputStream(Connection.getInputStream())) {
Stream.readFully(Buffer);
}
return new String(Buffer);
} catch (Exception Ex) {
return null;
}
}

//OR

public static String getPage(URL Address) {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");

try (BufferedReader Stream = new BufferedReader(new InputStreamReader(Connection.getInputStream()))) {
String Line, Result = new String();
while ((Line = Stream.readLine()) != null) {
Result += Line;
}
return Result;
}
} catch (Exception Ex) {
System.out.println(Ex.getMessage());
}
return null;
}

The above can be used like this small example which contains what I assume to be the event handling you want?:


package browser;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Browser {

private JFrame frame = null;
private JTextField AddressBar = null;
private JButton GoButton = null;
private JTextArea ViewArea = null;

public static void main(String[] args) {
Browser B = new Browser();
B.initialize();
B.frame.setVisible(true);
}

private void initialize() {
frame = new JFrame("Some Browser..");
frame.setBounds(100, 100, 550, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel AddressPanel = new JPanel();
JPanel ViewPanel = new JPanel();
AddressPanel.add(AddressBar = new JTextField());
AddressPanel.add(GoButton = new JButton("Go"));
ViewPanel.add(ViewArea = new JTextArea(25, 25));
frame.add(AddressPanel, BorderLayout.NORTH);
frame.add(ViewPanel, BorderLayout.SOUTH);
AddressBar.setColumns(45);
ViewArea.setLineWrap(true);
ViewArea.setEditable(false);
ViewArea.setAutoscrolls(true);
JScrollPane scroll = new JScrollPane(ViewArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll);
frame.pack();
this.addListeners();
}

private void addListeners() {
GoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String Link = AddressBar.getText();
if (Link == null) {
System.out.println("Cannot search for blank URLs!");
} else {
try {
String Source = getPage(new URL(Link));
if (Source != null && !Source.isEmpty()) {
ViewArea.setText(Source);
} else {
System.out.println("Failed to retrieve Source..");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
});

AddressBar.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
GoButton.doClick();
}
}
});
}

public static void downloadFile(URL Address, String Path) throws IOException {
URLConnection Connection = Address.openConnection();
try (FileOutputStream OutFile = new FileOutputStream(Path); InputStream IStream = Connection.getInputStream()) {
byte[] Buffer = new byte[1024];
for (int Read = 0; (Read = IStream.read(Buffer)) != -1;) {
OutFile.write(Buffer, 0, Read);
}
}
}

public static String getPage(URL Address) {
try {
URLConnection Connection = Address.openConnection();
Connection.addRequestProperty("Protocol", "HTTP/1.1");
Connection.addRequestProperty("Connection", "keep-alive");
Connection.addRequestProperty("Keep-Alive", "200");
Connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");

try (BufferedReader Stream = new BufferedReader(new InputStreamReader(Connection.getInputStream()))) {
String Line, Result = new String();
while ((Line = Stream.readLine()) != null) {
Result += Line;
}
return Result;
}
} catch (Exception Ex) {
System.out.println(Ex.getMessage());
}
return null;
}
}

Le Jingle

2013-05-03 01:45:26

Err yeah, I'm not certain to what your asking via the main post question. Are you trying to add a pop up for entering a URL instead of entering one inside the class?

Also, is the class assignments page a publicly accessible link?

J J

2013-05-04 14:06:07

Interesting to see how universities around the world have the same sort of program - atleast ours matches a lot.
We need to make an Android Twitter Client and in the first week we also worked with connections and getting the result code eg. 201 = succesful and 500 is some sort of error etc. and you needed to debug those. Now we had a week of holiday inbetween and monday the second week starts and we need to make something similar I believe.

With the methods @Brandon; provided you should be able to finish it relatively easily now :)

[XoL]

2013-05-04 15:42:39

I finished it using a different method, but yeah that helped!