Java Trying to get a line of source from a website
- by dsta
I'm trying to get one line of source from a website and then I'm returning that line back to main. I keep on getting an error at the line where I define InputStream in. Why am I getting an error at this line?
public class MP3LinkRetriever
{
private static String line;
public static void main(String[] args)
{
String link = "www.google.com";
String line = "";
while (link != "")
{
link = JOptionPane.showInputDialog("Please enter the link");
try
{
line = Connect(link);
}
catch(Exception e)
{
}
JOptionPane.showMessageDialog(null, "MP3 Link: " + parseLine(line));
String text = line;
Toolkit.getDefaultToolkit( ).getSystemClipboard()
.setContents(new StringSelection(text), new ClipboardOwner()
{
public void lostOwnership(Clipboard c, Transferable t) { }
});
JOptionPane.showMessageDialog(null, "Link copied to your clipboard");
}
}
public static String Connect(String link) throws Exception {
String strLine = null;
InputStream in = null;
try {
URL url = new URL(link);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(uc.getInputStream());
Reader re = new InputStreamReader(in);
BufferedReader r = new BufferedReader(re);
int index = -1;
while ((strLine = r.readLine()) != null && index == -1) {
index = strLine.indexOf("<source src");
}
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return strLine;
}
public static String parseLine(String line)
{
line = line.replace("<source", "");
line = line.replace(" src=", "");
line = line.replace("\"", "");
line = line.replace("type=", "");
line = line.replace("audio/mpeg", "");
line = line.replace(">", "");
return line;
}
}