如何阅读 java 中的简单播放列表

How to read a simple playlist in java

我正在尝试创建一个简单的 java 命令行代码,它将在命令行中接受来自播放列表的 URL 并且它应该 return 播放列表内容。

我收到以下回复 在此处输入播放列表 url(0 表示退出): http://gv8748.lu.edu:8084/sweng987/simple-01/playlist.m3u8 java.net.MalformedURLException:无协议:http://lu8748.lu.edu:8084/sweng987/simple-01/playlist.m3u8

at java.base/java.net.URL.<init>(URL.java:627)
at java.base/java.net.URL.<init>(URL.java:523)
at java.base/java.net.URL.<init>(URL.java:470)
at edu.lu.sweng987.SimplePlaylist.getPlaylistUrl(SimplePlaylist.java:36)
at edu.lu.sweng987.SimplePlaylist.main(SimplePlaylist.java:21)

我的代码如下

package edu.psgv.sweng861;
import java.util.Scanner; 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;

public class SimplePlaylist {

 private SimplePlaylist() {
  //don't allow instances
 }
 // The main function returns the URL entered 
 public static void main(String[] args) throws IOException{ 
  String output = getPlaylistUrl("");
  System.out.println(output);
  
  
 }
 

 private static String getPlaylistUrl(String theUrl) {
  String content = "";
  Scanner scanner = new Scanner(System.in);
  boolean validInput = false; 

  System.out.println("Enter playlist url here (0 to quit):");
   content = scanner.nextLine();
  try {
   URL url = new URL(theUrl);
   URLConnection urlConnection = (HttpURLConnection) url.openConnection();
   BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
   String line;
   
   while ((line = bufferedReader.readLine()) != null) {
    content += line + "\n";
   }
   bufferedReader.close();
  } catch(Exception e) {
   
   e.printStackTrace();
  }  
  return content;
 }

 
 
}

您在创建 URL 实例时错误地使用了方法的参数,而不是实际包含 url

的局部变量

改变

content = scanner.nextLine();
try {
   URL url = new URL(theUrl);

content = scanner.nextLine();
try {
   URL url = new URL(content);