【问题标题】:Parsing SHOUTcast 7.html metadata on Android在 Android 上解析 SHOUTcast 7.html 元数据
【发布时间】:2014-02-13 22:34:35
【问题描述】:

我正在尝试使用此 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");
}


}

关于我做错了什么有什么想法吗?

【问题讨论】:

  • 顺便说一句,我已经尝试过这个论坛和其他论坛的其他解决方案,但它们似乎有错误,可能是旧代码已被弃用。
  • DI 完成了我需要的操作,遵循此 stackoverflow.com/questions/9388560/… 显示此代码。 URL url = new URL("molestia.ponify.me:8062/7.html"); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0"); // 这个臭虫在这里拯救了一天!读者 r = new InputStreamReader(con.getInputStream()); StringBuilder buf = new StringBuilder(); while (true) { int ch = r.read(); if (ch
  • 对不起,我的最后一条评论格式不正确,我正在使用电话,按回车键并没有给我一个新行来为代码添加空格。
  • 太好了,非常感谢老兄,我正在努力让类似的代码工作,你是对的,所有可用的代码都不起作用,你的代码工作完美,干杯! :D 顺便说一句,您可能应该将其添加为正确的答案,并使用正确的格式,以便其他人可以从中受益:)

标签: android html parsing shoutcast


【解决方案1】:

这是 Pulsarman325 的工作解决方案,经过整理,我必须添加一些额外的东西才能使其工作(尝试/捕获和变量初始化)

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

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多