试图从网站获取一系列来源

时间:2012-09-24 00:31:22

标签: java html

我正在尝试从网站获取一行来源,然后我将该行返回到main。我继续在我定义InputStream的行中收到错误。为什么我在此行收到错误?

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;
}

}

2 个答案:

答案 0 :(得分:3)

strLine = r.readLine();会在到达内容结尾时返回null,但在您阅读该行之后,您不会检查null的可能性。你在你的代码中使用它......

尝试类似......

while ((strLine = r.readLine()) != null && index == -1) {
    index = strLine.indexOf("<source src");
}

另外,在离开方法之前,请确保关闭蒸汽......

InputStream in = null;
try {
    // The rest of your code...
} finally {
    try {
        in.close();
    } catch (Exception exp) { // We don't really care about this one..
    }
}

更新 - 完整示例

这是我使用的测试代码,工作正常......

public class TestWebDownload {

    public static void main(String[] args) {
        try {
            Connect("http://www.google.com.au");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    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)
{

    // What if line is null ??
    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;
}

答案 1 :(得分:0)

更新

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class Example {    
    public static void main(String[] args) {
        String strLine = "Line never found";
        try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL("page").openStream()))){
            while ((strLine = in.readLine()) != null) {
                if (strLine.indexOf("<source src") > 0) {
                    break;
                }
            }
            in.close();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURL catched");
        } catch (IOException e) {
            System.out.println("InputOutputException catched");
        } 
    }
}