我正在尝试使用此 URL 检查 SHOUTcast 流的状态:
http://85.17.167.136:8684/7.html
...返回如下数据:
<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>7,1,77,100,7,128,+44(0)7908 340 811 Follow Us @visionradiouk</body></html>
我知道,如果流已启动并正在运行,则第一个逗号之后返回 1,如果流已关闭,则返回 0。我的问题是获取该页面的 html?我使用此代码,它适用于 Google 等其他网站。
TextView tView = (TextView) findViewById(R.id.textView1);
String htmlCode = "";
try {
URL url = new URL("http://85.17.167.136:8684/7.html");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine())!= null)
htmlCode += inputLine;
System.out.println(htmlCode);
tView.setText(htmlCode);
in.close();
} catch (Exception e){
System.out.println("error");
}
}
对我做错了什么有什么想法吗?
最佳答案
这是 Pulsarman325 的工作解决方案,经过整理,我必须添加一些额外的东西才能让它工作(try/catch 和变量初始化)
String url = "http://molestia.ponify.me:8062";
URL url2=null;
try
{
url2 = new URL(url + "/7.html");
}
catch (MalformedURLException e1)
{
e1.printStackTrace();
}
URLConnection con=null;
try
{
con = url2.openConnection();
}
catch (IOException e1)
{
e1.printStackTrace();
}
con.setRequestProperty("User-Agent", "Mozilla/5.0");
Reader r = null;
try
{
r = new InputStreamReader(con.getInputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
StringBuilder buf = new StringBuilder();
int ch=0;
while (true)
{
try
{
ch = r.read();
}
catch (IOException e)
{
e.printStackTrace();
}
if (ch < 0)
break;
buf.append((char) ch);
}
String str = buf.toString();
String trackinfo = str.split(",")[6].split("</body>")[0];
Log.d("HTML", trackinfo);
关于android - 在 Android 上解析 SHOUTcast 7.html 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767015/