gpt4 book ai didi

java - 尝试从网站获取一行源代码

转载 作者:行者123 更新时间:2023-12-02 00:12:12 25 4
gpt4 key购买 nike

我正在尝试从网站获取一行源代码,然后将该行返回到主程序。我在定义 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;
}

}

最佳答案

strLine = r.readLine();将返回null当它到达内容末尾时,但您没有检查 null 的可能性在您阅读该行之后并在代码中使用它之前...

尝试类似...

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

此外,请确保在离开方法之前关闭 Steam ...

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

关于java - 尝试从网站获取一行源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12557471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com